现在我们已经了解了如何注册和使用一个Bean,那么,如何向Bean的成员属性进行赋值呢?也就是说,IoC在创建对象时,需要将我们预先给定的属性注入到对象中,非常简单,我们可以使用property标签来实现,但是一定注意,此属性必须存在一个set方法,否则无法赋值:
- <bean name="student" class="com.test.bean.Student">
- <property name="name" value="小明"/>
- bean>
- public class Student {
- String name;
- int age;
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void say(){
- System.out.println("我是:"+name);
- }
- }
最后测试是否能够成功将属性注入到我们的对象中:
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
- Student student = (Student) context.getBean("student");
- student.say();
- }
那么,如果成员属性是一个非基本类型非String的对象类型,我们该怎么注入呢?
- public class Card {
- }
- public class Student {
- String name;
- int age;
- Card card;
-
- public void setCard(Card card) {
- this.card = card;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void say(){
- System.out.println("我是:"+name+",我都学生证:"+card);
- }
- }
我们只需要将对应的类型也注册为bean即可,然后直接使用ref属性来进行引用:
- <bean name="card" class="com.test.bean.Card"/>
- <bean name="student" class="com.test.bean.Student">
- <property name="name" value="小明"/>
- <property name="card" ref="card"/>
- bean>
那么,集合如何实现注入呢?我们需要在property内部进行编写:
- <bean name="student" class="com.test.bean.Student">
- <property name="list">
- <list>
- <value type="double">100.0value>
- <value type="double">95.0value>
- <value type="double">92.5value>
- list>
- property>
- bean>
现在,我们就可以直接以一个数组的方式将属性注入,注意如果是List类型的话,我们也可以使用array数组。同样的,如果是一个Map类型,我们也可以使用entry来注入:
- public class Student {
- String name;
- int age;
- Map
map; -
- public void setMap(Map
map) { - this.map = map;
- }
-
- public void say(){
- System.out.println("我的成绩:"+ map);
- }
- }
- <bean name="student" class="com.test.bean.Student">
- <property name="map">
- <map>
- <entry key="语文" value="100.0"/>
- <entry key="数学" value="80.0"/>
- <entry key="英语" value="92.5"/>
- map>
- property>
- bean>
我们还可以使用自动装配来实现属性值的注入:
- <bean name="card" class="com.test.bean.Card"/>
- <bean name="student" class="com.test.bean.Student" autowire="byType"/>
自动装配会根据set方法中需要的类型,自动在容器中查找是否存在对应类型或是对应名称以及对应构造方法的Bean,比如我们上面指定的为byType,那么其中的card属性就会被自动注入类型为Card的Bean
我们已经了解了如何使用set方法来创建对象,那么能否不使用默认的无参构造方法,而是指定一个有参构造进行对象的创建呢?我们可以指定构造方法:
- <bean name="student" class="com.test.bean.Student">
- <constructor-arg name="name" value="小明"/>
- <constructor-arg index="1" value="18"/>
- bean>
- public class Student {
- String name;
- int age;
-
- public Student(String name, int age){
- this.name = name;
- this.age = age;
- }
-
- public void say(){
- System.out.println("我是:"+name+"今年"+age+"岁了!");
- }
- }
通过手动指定构造方法参数,我们就可以直接告诉容器使用哪一个构造方法来创建对象。