注意细节。
一、知识点
(1)单纯的类型错误
1 2 3 4 5 6 7 |
<bean id="god" class="spring.god"> <constructor-arg value="godlikexie" type="java.lang.String"></constructor-arg> <constructor-arg type="int"> <value>666</value> </constructor-arg> <constructor-arg value="create"></constructor-arg> </bean> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package spring; public class god { private String name; private String age; private String ability; public god(String name, String age, String ability) { super(); this.name = name; this.age = age; this.ability = ability; } @Override public String toString() { return "god [name=" + name + ", age=" + age + ", ability=" + ability + "]"; } } |
配置的(你注入的)数据类型指定为int了,但是你bean里数据类型的是string,报错:告诉你数据类型不正确了。
1 |
Unsatisfied dependency expressed through constructor argument with index 2 of type [java.lang.String]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments? |
同理,配置的(你注入的)数据类型指定为string,但是你bean里数据类型的是int,报错:无法将string类型转为int类型。
1 |
Unsatisfied dependency expressed through constructor argument with index 1 of type [int]: Could not convert constructor argument value of type [java.lang.String] to required type [int]: Failed to convert value of type [java.lang.String] to required type [int]; nested exception is java.lang.NumberFormatException: For input string: "create" |
(2)数据类型细节
记住string类型应该是java.lang.String。
string在java里需要接口实现,数据类型是一个类,所以不能写单纯的string。
但是int可以。
1 2 3 4 5 6 7 |
<bean id="god" class="spring.god"> <constructor-arg value="godlikexie" type="java.lang.String"></constructor-arg> <constructor-arg type="string"> <value>666</value> </constructor-arg> <constructor-arg value="create"></constructor-arg> </bean> |
当你写string的时候,报错:错误类型,构造器错误。
1 |
Unsatisfied dependency expressed through constructor argument with index 2 of type [java.lang.String]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments? |
改回java.lang.String才能正确注入bean。
(3)特殊符号
平时java程序里String test = “<666>”是被允许的,但是在xml文件里<>本身是特殊符号,怎么进行输出?
1 2 3 4 5 6 7 |
<bean id="god" class="spring.god"> <constructor-arg value="godlikexie" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><666></value> </constructor-arg> <constructor-arg value="create"></constructor-arg> </bean> |
尝试输出的时候会报错:内容错误。
1 |
nested exception is org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 12; 元素内容必须由格式正确的字符数据或标记组成。 |
正确的输出方法:
1 2 3 4 5 6 7 |
<bean id="god" class="spring.god"> <constructor-arg value="godlikexie" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<666>]]></value> </constructor-arg> <constructor-arg value="create"></constructor-arg> </bean> |
用<![CDATA[*****]]>包裹你要输出的string。
这样就可以正常输出了。
(4)类中引用其他类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package spring; public class xie { private String name; private god god; public String getName() { return name; } public void setName(String name) { this.name = name; } public god getGod() { return god; } public void setGod(god god) { this.god = god; } @Override public String toString() { return "xie [name=" + name + ", god=" + god + "]"; } } |
1 2 3 4 5 6 7 8 |
<bean id="xie" class="spring.xie"> <property name="name"> <value>iamgod</value> </property> <property name="god"> <ref bean="god"/> </property> </bean> |
引用god的bean。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class main { public static void main(String[] args) { ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\javaworkplace\\2.26spring\\src\\config.xml"); god god = (spring.god) applicationContext.getBean("god"); xie xie = (spring.xie) applicationContext.getBean("xie"); System.out.println(god.toString()); System.out.println(xie.toString()); } } |
运行结果:
1 2 |
god [name=godlikexie, age=<666>, ability=create] xie [name=iamgod, god=god [name=godlikexie, age=<666>, ability=create]] |
<ref/>标签或者ref属性都能成功在类中引用另外一个类。
(5)创建内部类
1 2 3 4 5 6 7 8 9 10 11 12 |
<bean id="xie" class="spring.xie"> <property name="name"> <value>iamgod</value> </property> <property name="god"> <bean class="spring.god"> <constructor-arg value="xie"></constructor-arg> <constructor-arg value="233"></constructor-arg> <constructor-arg value="showoff"></constructor-arg> </bean> </property> </bean> |
- 因为god是xie中的一个元素,所以先设为<property>。
- name是从xie类中定义的god god来的,所以name=”god”。
- 因为god也是一个bean,所以在<property>里面要新定义一个bean,然后设值。
当然要这样也是可以的:
1 2 3 4 5 6 7 8 9 10 11 12 |
<bean id="xie" class="spring.xie"> <property name="name"> <value>iamgod</value> </property> <property name="god"> <bean class="spring.god"> <property name="name" value="xie"></property> <property name="age" value="233"></property> <property name="ability" value="showoff"></property> </bean> </property> </bean> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class main { public static void main(String[] args) { ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\javaworkplace\\2.26spring\\src\\config.xml"); xie xie = (spring.xie) applicationContext.getBean("xie"); System.out.println(xie.toString()); } } |
运行结果:
1 |
xie [name=iamgod, god=god [name=xie, age=233, ability=showoff]] |
正确定义了内部类,并且正确输出出来了。
但是要注意,这个内部bean是不能被外部引用的。
(6)赋予null值
其实这个问题关系不大,因为bean中只要不赋值就是null的,不用特地弄个null。
但是有些情况可能想让逻辑更加清晰,那就有必要试一下。
1 2 3 4 5 6 7 8 |
<property name="god"> <bean class="spring.god"> <property name="name" value="xie"></property> <property name="age"><null></null></property> <property name="ability" value="showoff"></property> </bean> </property> </bean> |
<null></null>就可以。
这样也可以:
1 2 3 4 5 6 7 8 9 |
<property name="god"> <bean class="spring.god"> <constructor-arg value="xie"></constructor-arg> <constructor-arg value="233"></constructor-arg> <constructor-arg> <null></null> </constructor-arg> </bean> </property> |
二、总结
记录一下。