Skip to content

Commit 0e41546

Browse files
committed
🐛 fix create beanDefinition with null ref. because the beanDefinition.lazyInit default value is set error
1 parent 9c1ae1b commit 0e41546

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BeanDefinition {
1313
String SCOPE_SINGLETON = "singleton";
1414
String SCOPE_PROTOTYPE = "prototype";
1515

16-
private boolean lazyInit = false;
16+
private boolean lazyInit = true;
1717
private String[] dependsOn;
1818
private ArgumentValues constructorArgumentValues;
1919
private PropertyValues propertyValues;

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,35 @@ public void loadBeanDefinitions(Resource resource) {
3636
String className = element.attributeValue("class");
3737
BeanDefinition beanDefinition = new BeanDefinition(id, className);
3838

39-
// 解析xml文件中property节点 将其映射到PropertyValues中
39+
// 处理constructor-arg节点
40+
List<Element> constructorElements = element.elements("constructor-arg");
41+
ArgumentValues argumentValues = new ArgumentValues();
42+
for (Element constructorElement : constructorElements) {
43+
String constructorType = constructorElement.attributeValue("type");
44+
String constructorName = constructorElement.attributeValue("name");
45+
String constructorValue = constructorElement.attributeValue("value");
46+
47+
argumentValues.addArgumentValues(new ArgumentValue(constructorType, constructorName, constructorValue));
48+
}
49+
beanDefinition.setConstructorArgumentValues(argumentValues);
50+
51+
// 解析properties中属性
4052
List<Element> propertyElements = element.elements("property");
4153
PropertyValues propertyValues = new PropertyValues();
54+
4255
List<String> refs = new ArrayList<>();
4356
for (Element propertyElement : propertyElements) {
4457
// 获取Property节点
4558
String propertyType = propertyElement.attributeValue("type");
4659
String propertyName = propertyElement.attributeValue("name");
4760
String propertyValue = propertyElement.attributeValue("value");
4861
String propertyRef = propertyElement.attributeValue("ref");
49-
String refValue;
62+
String refValue = "";
5063
boolean isRef = false;
5164
// 解析property标签后 获取ref节点参数 并且根据是否存在存在ref参数来设置PropertyValue,通过isRef标记一个bean是否引用了另外的bean
5265
if (propertyValue != null && !propertyValue.equals("")) {
5366
refValue = propertyValue;
54-
} else {
67+
} else if (propertyRef != null && !propertyRef.equals("")) {
5568
isRef = true;
5669
refValue = propertyRef;
5770
refs.add(refValue);
@@ -61,19 +74,8 @@ public void loadBeanDefinitions(Resource resource) {
6174
}
6275
// 将PropertyValues放入BeanDefinition中 以便后续进行实例化
6376
beanDefinition.setPropertyValues(propertyValues);
64-
65-
// 获取constructor-arg节点
66-
List<Element> constructorElements = element.elements("constructor-arg");
67-
ArgumentValues argumentValues = new ArgumentValues();
68-
for (Element constructorElement : constructorElements) {
69-
String constructorType = constructorElement.attributeValue("type");
70-
String constructorName = constructorElement.attributeValue("name");
71-
String constructorValue = constructorElement.attributeValue("value");
72-
73-
argumentValues.addArgumentValues(new ArgumentValue(constructorType, constructorName, constructorValue));
74-
}
75-
beanDefinition.setConstructorArgumentValues(argumentValues);
7677
beanDefinition.setDependsOn(refs.toArray(new String[0]));
78+
7779
// 正式注册BeanDefinition
7880
this.simpleBeanFactory.registerBeanDefinition(id, beanDefinition);
7981
}

src/test/java/com/dianpoint/summer/test/service/OtherOneService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ public void sayHello() {
99
otherTwoService.sayHello();
1010
}
1111

12+
public OtherTwoService getOtherTwoService() {
13+
return otherTwoService;
14+
}
15+
16+
public void setOtherTwoService(OtherTwoService otherTwoService) {
17+
this.otherTwoService = otherTwoService;
18+
}
1219
}

src/test/java/com/dianpoint/summer/test/service/OtherTwoService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ void sayHello() {
1111
void sayBye(){
1212
simpleService.sayBye();
1313
}
14+
15+
public SimpleService getSimpleService() {
16+
return simpleService;
17+
}
18+
19+
public void setSimpleService(SimpleService simpleService) {
20+
this.simpleService = simpleService;
21+
}
1422
}

0 commit comments

Comments
 (0)