目录
一、给Bean的属性赋值
1.1 创建测试类
Address.java
public class Address {
private String province;
private String city;
}
Book.java
public class Book {
private Integer bid;
private String bname;
}
Student.java
public class Student {
private Integer sid;
private double height;
private String sname;
private Address address;
private List<String> pens;
private List<Book> books;
private Map<String,Object> score;
}
分别生成对应属性的get、set、toString方法。
SpringBeanTest.java
public class SpringBeanTest {
public static void main(String[] args) {
// 1.创建IOC容器,并指定配置文件位置
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.从IOC容器中获取管理的对象
Student bean = ac.getBean("s1",Student.class);
// 3.输出验证
System.out.println(bean);
}
}
2.1 通过标签赋值
2.1.1 基本数类型、包装类赋值
<bean id="s1" class="com.wyl.spring.Student">
<property name="sid" value="10001"></property>
<property name="height" value="180"></property>
<property name="sname" value="张三"></property>
</bean>
控制台输出
Student{sid=10001, height=180.0, sname='张三', address=null, pens=null, books=null, score=null}
2.1.2 引用类型赋值
2.1.2.1 引用外部Bean
<bean id="address" class="com.wyl.spring.Address">
<property name="province" value="河北"></property>
<property name="city" value="唐山"></property>
</bean>
<bean id="s2" class="com.wyl.spring.Student">
<property name="address" ref="address"></property>
</bean>
Student对象的创建需要依赖一个Address对象,将Address类也交给Spring管理,让Spring自动帮我们创建好Address对象,这样在有依赖着Address对象的地方,直接使用标签的ref属性可以直接引用外部创建的Bean。
控制台输出
Student{sid=null, height=0.0, sname='null', address=Address{province='河北', city='唐山'}, pens=null, books=null, score=null}
还可以通过级联属性,对引入外部Bean的属性赋值和修改
<bean id="s2" class="com.wyl.spring.Student">
<property name="address" ref="address"></property>
<property name="address.city" value="邢台"></property>
</bean>
控制台输出
Student{sid=null, height=0.0, sname='null', address=Address{province='河北', city='邢台'}, pens=null, books=null, score=null}
2.1.2.2 创建内部Bean
<bean id="s3" class="com.wyl.spring.Student">
<property name="address">
<bean class="com.wyl.spring.Address">
<property name="province" value="辽宁"></property>
<property name="city" value="沈阳"></property>
</bean>
</property>
</bean>
内部Bean的作用范围仅限于当前Bean使用,不能被其他Bean引用,所以id属性可有可无。
控制台输出
Student{sid=null, height=0.0, sname='null', address=Address{province='辽宁', city='沈阳'}, pens=null, books=null, score=null}
2.1.3 集合类型赋值
2.1.3.1 List类型
<bean id="s4" class="com.wyl.spring.Student">
<property name="pens">
<list>
<value>铅笔</value>
<value>钢笔</value>
<value>毛笔</value>
</list>
</property>
</bean>
使用标签,在标签内部使用标签指定集合中的值。因为ArrList底层就是数组,所以集合和数组赋值都是这种方式。
控制台输出
Student{sid=null, height=0.0, sname='null', address=null, pens=[铅笔, 钢笔, 毛笔], books=null, score=null}
如果集合中存放的引用数据类型,可以将对应的类交给Spring管理,然后通过标签的bean属性引用外部创建的Bean。
<bean id="b1" class="com.wyl.spring.Book">
<property name="bname" value="语文"></property>
</bean>
<bean id="b2" class="com.wyl.spring.Book">
<property name="bname" value="数学"></property>
</bean>
<bean id="s5" class="com.wyl.spring.Student">
<property name="books">
<list>
<ref bean="b1"></ref>
<ref bean="b2"></ref>
</list>
</property>
</bean>
还可以使用utils:list标签将集合抽取成外部的Bean,然后在使用到集合的地方,引用外部的定义好的集合即可。
<utils:list id="penList">
<value>铅笔</value>
<value>钢笔</value>
<value>毛笔</value>
</utils:list>
<bean id="s6" class="com.wyl.spring.Student">
<property name="pens" ref="penList"></property>
</bean>
控制台输出
Student{sid=null, height=0.0, sname='null', address=null, pens=[铅笔, 钢笔, 毛笔], books=null, score=null}
2.1.3.2 Map类型
<bean id="s7" class="com.wyl.spring.Student">
<property name="score">
<map>
<entry key="数学" value="99"></entry>
<entry key="语文" value="95"></entry>
<entry key="英语" value="98"></entry>
<entry key="物理" value="94"></entry>
</map>
</property>
</bean>
Map的底层是一个个的entry,所以可以使用标签来为map中指定一个键值对。
控制台输出
Student{sid=null, height=0.0, sname='null', address=null, pens=null, books=null, score={数学=99, 语文=95, 英语=98, 物理=94}}
Map类型也可以使用utils:map来抽取一个外部Map,也可以用 标签的 key-ref属性,引用一个外部的Bean作为Key;value-reff属性,引用一个外部的Bean作为value值。
二、可以使用的值
通过以上案例,可以看出为属性赋值可以使用以下几种值
2.1 字面量
在标签的value属性中,可以用字符串直接表示的值,可以用于基本数据类型以及其对应包装类、String类型赋值。
<bean id="s1" class="com.wyl.spring.Student">
<property name="sid" value="10001"></property>
<property name="height" value="180"></property>
<property name="sname" value="张三"></property>
</bean>
2.1 外部Bean
一个Bean的属性是另一个类的对象,可以引用Spring所管理的Bean,为这个属性赋值。
<bean id="address" class="com.wyl.spring.Address">
<property name="province" value="河北"></property>
<property name="city" value="唐山"></property>
</bean>
<bean id="s2" class="com.wyl.spring.Student">
<!--通过ref属性引用一个id为address的Bean-->
<property name="address" ref="address"></property>
</bean>
2.1 内部Bean
在一个Bean的内部创建一个Bean,为属性赋值,内部bean不能被其他Bean所引用。
<bean id="s3" class="com.wyl.spring.Student">
<property name="address">
<bean class="com.wyl.spring.Address">
<property name="province" value="辽宁"></property>
<property name="city" value="沈阳"></property>
</bean>
</property>
</bean>