Skip to content

Commit 74da503

Browse files
authored
Merge pull request dianpoint#8 from dianpoint/enhance-beanDefinition
Enhance bean definition
2 parents 13d3132 + 9c04f43 commit 74da503

21 files changed

+821
-41
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.dianpoint.summer.beans;
2+
3+
/**
4+
* <p>
5+
* 定义BeanDefinition在xml文件中的格式,包括value|type|name属性
6+
* </p>
7+
*
8+
* @author: congcong
9+
* @email: congccoder@gmail.com
10+
* @date: 2023/3/17 16:58
11+
*/
12+
public class ArgumentValue {
13+
14+
private Object value;
15+
16+
private String name;
17+
18+
private String type;
19+
20+
public ArgumentValue(String type, Object value) {
21+
this.value = value;
22+
this.type = type;
23+
}
24+
25+
public ArgumentValue(String type, String name, Object value) {
26+
this.value = value;
27+
this.name = name;
28+
this.type = type;
29+
}
30+
31+
public Object getValue() {
32+
return value;
33+
}
34+
35+
public void setValue(Object value) {
36+
this.value = value;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getType() {
48+
return type;
49+
}
50+
51+
public void setType(String type) {
52+
this.type = type;
53+
}
54+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.dianpoint.summer.beans;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* <p>
8+
* 参数值封装对象,提供了 ArgumentValues 和 PropertyValues 两个类,封装、 增加、获取、判断等操作方法
9+
* </p>
10+
*
11+
* @author: congcong
12+
* @email: congccoder@gmail.com
13+
* @date: 2023/3/17 16:58
14+
*/
15+
public class ArgumentValues {
16+
17+
private List<ArgumentValue> argumentValues = new LinkedList<>();
18+
19+
public ArgumentValues() {}
20+
21+
/**
22+
* 根据Index索引顺序获取参数
23+
*
24+
* @param index
25+
* 参数索引位置
26+
* @return 返回参数类型
27+
*/
28+
public ArgumentValue getIndexedArgumentValue(int index) {
29+
return this.argumentValues.get(index);
30+
}
31+
32+
public int getArgumentCount() {
33+
return this.argumentValues.size();
34+
}
35+
36+
public boolean isEmpty() {
37+
return this.argumentValues.isEmpty();
38+
}
39+
40+
public void addArgumentValues(ArgumentValue argumentValue) {
41+
this.argumentValues.add(argumentValue);
42+
}
43+
}

src/main/java/com/dianpoint/summer/beans/BeanDefinition.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,82 @@
11
package com.dianpoint.summer.beans;
22

33
/**
4-
* <p>Bean的定义</p>
4+
* <p>
5+
* Bean的定义
6+
* </p>
57
*
68
* @author: congcong
79
* @email: congccoder@gmail.com
810
* @date: 2023/3/17 11:51
911
*/
1012
public class BeanDefinition {
13+
String SCOPE_SINGLETON = "singleton";
14+
String SCOPE_PROTOTYPE = "prototype";
15+
16+
private boolean lazyInit = true;
17+
private String[] dependsOn;
18+
private ArgumentValues constructorArgumentValues;
19+
private PropertyValues propertyValues;
20+
private String initMethodName;
21+
private volatile Object beanClass;
1122
private String id;
1223
private String className;
1324

25+
private String scope = SCOPE_SINGLETON;
26+
1427
public BeanDefinition(String id, String className) {
1528
this.id = id;
1629
this.className = className;
1730
}
1831

32+
public boolean isLazyInit() {
33+
return lazyInit;
34+
}
35+
36+
public void setLazyInit(boolean lazyInit) {
37+
this.lazyInit = lazyInit;
38+
}
39+
40+
public String[] getDependsOn() {
41+
return dependsOn;
42+
}
43+
44+
public void setDependsOn(String... dependsOn) {
45+
this.dependsOn = dependsOn;
46+
}
47+
48+
public ArgumentValues getConstructorArgumentValues() {
49+
return constructorArgumentValues;
50+
}
51+
52+
public void setConstructorArgumentValues(ArgumentValues constructorArgumentValues) {
53+
this.constructorArgumentValues = constructorArgumentValues;
54+
}
55+
56+
public PropertyValues getPropertyValues() {
57+
return propertyValues;
58+
}
59+
60+
public void setPropertyValues(PropertyValues propertyValues) {
61+
this.propertyValues = propertyValues;
62+
}
63+
64+
public String getInitMethodName() {
65+
return initMethodName;
66+
}
67+
68+
public void setInitMethodName(String initMethodName) {
69+
this.initMethodName = initMethodName;
70+
}
71+
72+
public Class<?> getBeanClass() {
73+
return (Class<?>)beanClass;
74+
}
75+
76+
public void setBeanClass(Class<?> beanClass) {
77+
this.beanClass = beanClass;
78+
}
79+
1980
public String getId() {
2081
return id;
2182
}
@@ -31,4 +92,20 @@ public String getClassName() {
3192
public void setClassName(String className) {
3293
this.className = className;
3394
}
95+
96+
public String getScope() {
97+
return scope;
98+
}
99+
100+
public void setScope(String scope) {
101+
this.scope = scope;
102+
}
103+
104+
public boolean isSingleton() {
105+
return SCOPE_SINGLETON.equals(scope);
106+
}
107+
108+
public boolean isPrototype() {
109+
return SCOPE_PROTOTYPE.equals(scope);
110+
}
34111
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.dianpoint.summer.beans;
2+
3+
/**
4+
* @author: congcong
5+
* @email: congccoder@gmail.com
6+
* @date: 2023/3/17 15:20
7+
*/
8+
public interface BeanDefinitionRegistry {
9+
void registerBeanDefinition(String name, BeanDefinition beanDefinition);
10+
11+
void removeBeanDefinition(String name);
12+
13+
BeanDefinition getBeanDefinition(String name);
14+
15+
boolean containsBeanDefinition(String name);
16+
}

src/main/java/com/dianpoint/summer/beans/BeanFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
*/
1212
public interface BeanFactory {
1313

14-
Object getBean(String beanName) throws NoSuchBeanDefinitionException;
14+
Object getBean(String beanName) throws BeansException;
1515

16-
void registerBeanDefinition(BeanDefinition beanDefinition);
16+
boolean containsBean(String name);
17+
18+
void registerBean(String beanName, Object object);
19+
20+
boolean isSingleton(String name);
21+
22+
boolean isPrototype(String name);
23+
24+
Class<?> getType(String name);
1725
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.dianpoint.summer.beans;
2+
3+
/**
4+
* @author: congcong
5+
* @email: congccoder@gmail.com
6+
* @date: 2023/3/17 15:03
7+
*/
8+
public class BeansException extends Exception {
9+
public BeansException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.dianpoint.summer.beans;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
/**
9+
* @author: congcong
10+
* @email: congccoder@gmail.com
11+
* @date: 2023/3/17 15:12
12+
*/
13+
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
14+
15+
private List<String> beanNames = new ArrayList<>();
16+
private Map<String, Object> singletons = new ConcurrentHashMap<>(256);
17+
18+
@Override
19+
public void registerSingleton(String beanName, Object singletonObject) {
20+
// 保证BeanNames和singletons的操作的原子性
21+
synchronized (this.singletons) {
22+
this.singletons.put(beanName, singletonObject);
23+
this.beanNames.add(beanName);
24+
}
25+
}
26+
27+
@Override
28+
public Object getSingleton(String beanName) {
29+
return this.singletons.get(beanName);
30+
}
31+
32+
@Override
33+
public boolean containsSingleton(String beanName) {
34+
return this.singletons.containsKey(beanName);
35+
}
36+
37+
@Override
38+
public String[] getSingletonNames() {
39+
return (String[])this.beanNames.toArray();
40+
}
41+
42+
protected void removeSingleton(String beanName) {
43+
synchronized (this.singletons) {
44+
this.singletons.remove(beanName);
45+
this.beanNames.remove(beanName);
46+
}
47+
}
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.dianpoint.summer.beans;
2+
3+
/**
4+
* <p>
5+
* 用以保存BeanDefinition在xml文件中的定义,包括待注入的属性类型type、名称name、值value、引用类型ref
6+
* </p>
7+
*
8+
* @author: congcong
9+
* @email: congccoder@gmail.com
10+
* @date: 2023/3/17 17:00
11+
*/
12+
public class PropertyValue {
13+
14+
private final String type;
15+
private final String name;
16+
private final Object value;
17+
18+
/**
19+
* 属性是否是引用属性
20+
*/
21+
private final boolean isRef;
22+
23+
public PropertyValue(String type, String name, Object value, boolean isRef) {
24+
this.type = type;
25+
this.name = name;
26+
this.value = value;
27+
this.isRef = isRef;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public Object getValue() {
35+
return value;
36+
}
37+
38+
public String getType() {
39+
return type;
40+
}
41+
42+
public boolean isRef() {
43+
return isRef;
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.dianpoint.summer.beans;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* <p>
8+
* 解析Resource文件中Property属性节点,将其批量存储PropertyValues中
9+
* </p>
10+
*
11+
* @author: congcong
12+
* @email: congccoder@gmail.com
13+
* @date: 2023/3/17 17:00
14+
*/
15+
public class PropertyValues {
16+
17+
private List<PropertyValue> propertyValueList;
18+
19+
public PropertyValues() {
20+
this.propertyValueList = new ArrayList<>(0);
21+
}
22+
23+
/**
24+
* 设置propertyValue
25+
*
26+
* @param propertyValue
27+
* 资源属性值
28+
*/
29+
public void addPropertyValue(PropertyValue propertyValue) {
30+
this.propertyValueList.add(propertyValue);
31+
}
32+
33+
34+
public boolean isEmpty() {
35+
return this.propertyValueList.isEmpty();
36+
}
37+
38+
public int size() {
39+
return this.propertyValueList.size();
40+
}
41+
42+
public List<PropertyValue> getPropertyValueList() {
43+
return propertyValueList;
44+
}
45+
}

0 commit comments

Comments
 (0)