@@ -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 }
0 commit comments