各类属性赋值
通过p名称空间为Bean赋值
<bean id="person05" class="com.lwt.bean.Person" p:age="18" p:lastName="威彤" p:gender="男" p:email="lwt@qq.com">
</bean>
</beans>
@Test
public void test5() {
Person bean = ioc.getBean("person05/04/03/02/01", Person.class);
System.out.println(bean);
}
测试使用null
为默认的固定值再赋真正的null值
bean id="person01" class="com.lwt.bean.Person">
<property name="lastName">
<null></null>
</property>
</bean>
引用类型赋值
ref引用外部其他bean 和 在property中引用内部bean
以下是bean中套property,通过bean Id=car01获取car对象值
<!-- bean套property-->
<bean id="car01" class="com.lwt.bean.Car">
<property name="carName" value="宝马"></property>
<property name="color" value="白色"></property>
<property name="price" value="300000"></property>
</bean>
@Test
public void test1() {
Person bean = ioc.getBean("person01", Person.class);
System.out.println(bean);
System.out.println("修改前"+bean.getCar());
Car car = bean.getCar();
Car car01 = (Car)ioc.getBean("car01");
System.out.println(car01 == car);
car.setCarName("自行车");
System.out.println("修改后"+car01);
System.out.println(car01 == car); // true 同时修改 同时改变 所以仍保持相同
}
结果:
Person{lastName=‘null’, age=null, gender=‘null’, email=‘null’, car=Car{carName=‘宝马’, price=300000, color=‘白色’}, books=null, maps=null, properties=null}
修改前Car{carName=‘宝马’, price=300000, color=‘白色’}
true
修改后Car{carName=‘自行车’, price=300000, color=‘白色’}
true
集合类型赋值
List
<!-- 在<list>中使用<ref bean=id>-->
<bean id="book01" class="com.lwt.bean.Book">
<property name="bookName" value="spring三刷"></property>
</bean>
<bean id="person01" class="com.lwt.bean.Person">
<property name="books">
<!-- books = new ArrayList<Book>();-->
<list>
<bean id="xbook001x" class="com.lwt.bean.Book" p:author="彤儿" p:bookName="spring从入门到二刷"></bean>
<ref bean="book01"></ref>
</list>
</property>
Map
<property name="maps">
<!-- maps = new LinkedHashMap<>();-->
<map>
<!-- 一个entry代表一个键值对-->
<entry key="key1" value="value1"></entry>
<entry key="key2" value="value2"></entry>
<entry key="key3" value-ref="book01"></entry>
<entry key="key4">
<bean class="com.lwt.bean.Car">
<property name="carName" value="机车女孩的车"></property>
</bean>
</entry>
<entry key="key5">
<map>
<entry key="key51" value="嵌套map"></entry>
</map>
</entry>
</map>
</property>
@Test
public void test2() {
Person bean = (Person) ioc.getBean("person01");
Map<String, Object> maps = bean.getMaps();
System.out.println(maps);
System.out.println(bean.getProperties());
}
Properties
<property name="properties">
<!-- properties = new Properties();k=v都是string-->
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
@Test
public void test2() {
Person bean = (Person) ioc.getBean("person01");
Map<String, Object> maps = bean.getMaps();
System.out.println(maps);
System.out.println(bean.getProperties());
}
util名称空间创建集合类型bean
util名称空间创建集合类型的bean:方便引用map,list,properties等
在properties中使用ref引用
<bean id="person02" class="com.lwt.bean.Person">
<property name="maps" ref="myMap"></property>
</bean>
<!-- new LinkedHashMap<>();-->
<util:map id="myMap">
<entry key="key1" value="value1"></entry>
<entry key="key2" value="value2"></entry>
<entry key="key3" value-ref="book01"></entry>
<entry key="key4">
<bean class="com.lwt.bean.Car">
<property name="carName" value="机车女孩的车"></property>
</bean>
</entry>
<entry key="key5">
<map>
<entry key="key51" value="嵌套map"></entry>
</map>
</entry>
</util:map>
<!-- List集合中有四个元素[[],Person,666,{}]-->
<util:list id="myList">
<list></list>
<bean class="com.lwt.bean.Person"></bean>
<value>666</value>
<ref bean="myMap"></ref>
</util:list>
</beans>
@Test
public void test3() {
Person bean = (Person) ioc.getBean("person02");
Map<String, Object> maps = bean.getMaps();
System.out.println(maps);
Map<String, Object> myMap = (Map<String, Object>) ioc.getBean("myMap");
System.out.println(myMap.getClass());
}
结果 :
{key1=value1, key2=value2, key3=Book{bookName=‘spring三刷’, author=‘null’}, key4=Car{carName=‘机车女孩的车’, price=null, color=‘null’}, key5={key51=嵌套map}}
class java.util.LinkedHashMap