介绍spring-bean的引用、内部bean、赋值为null、级联属性赋值、集合、map
User.java:
package com.shuoeasy.test; public class User { private int id; private String name; public User(){} public User(String name){ this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void hello(){ System.out.println("你好:" + this.name); } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
Order.java:
package com.shuoeasy.test; import java.util.List; import java.util.Map; public class Order { private int id; private User user; private List<String> titles; private Map<String,String> maps; public Order(){} public Order(int id, User user){ this.id = id; this.user = user; } public int getId() { return id; } public void setId(int id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<String> getTitles() { return titles; } public void setTitles(List<String> titles) { this.titles = titles; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } @Override public String toString() { return "Order [id=" + id + ", user=" + user + ", titles=" + titles + ", maps=" + maps + "]"; } }
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 属性注入 --> <bean id="userHello" class="com.shuoeasy.test.User"> <property name="name" value="老鹰开灰机"></property> </bean> <!-- 构造注入 --> <bean id="seller" class="com.shuoeasy.test.Seller"> <constructor-arg type="String" value="我是商家"></constructor-arg> <constructor-arg type="double" value="5000"></constructor-arg> <constructor-arg type="boolean" value="false"></constructor-arg> </bean> <bean id="seller2" class="com.shuoeasy.test.Seller"> <constructor-arg type="String" > <value><![CDATA[我是含有非法字符串的<数据]]></value> </constructor-arg> <constructor-arg type="double" value="5000"></constructor-arg> </bean> <!-- bean的引用 --> <bean id="order" class="com.shuoeasy.test.Order"> <property name="id" value="1234"></property> <!-- 使用ref引用 --> <property name="user" ref="userHello" ></property> </bean> <!-- 内部bean --> <bean id="order2" class="com.shuoeasy.test.Order"> <property name="id" value="9527"></property> <property name="user"> <!-- 内部bean不能给外部引用 --> <bean class="com.shuoeasy.test.User"> <property name="name" value="内部bean"></property> </bean> </property> </bean> <!-- 赋值为null --> <bean id="order3" class="com.shuoeasy.test.Order"> <property name="id" value="5566"></property> <!-- 使用ref引用 --> <property name="user"><null/></property> </bean> <!-- 级联属性赋值 --> <bean id="user4" class="com.shuoeasy.test.User"> <constructor-arg name="name" value="级联属性"></constructor-arg> </bean> <bean id="order4" class="com.shuoeasy.test.Order"> <property name="id" value="7788"></property> <property name="user" ref="user4"></property> <!-- 级联 --> <property name="user.id" value="123"></property> <property name="user.name" value="说易事"></property> </bean> <!-- 集合 --> <bean id="order5" class="com.shuoeasy.test.Order" > <property name="id" value="7788"></property> <property name="user" ref="user4"></property> <property name="titles"> <list> <value>标题1</value> <value>标题2</value> <!-- 引用bean<ref bean="username"/> --> </list> </property> </bean> <!-- map --> <bean id="order6" class="com.shuoeasy.test.Order"> <property name="id" value="7788"></property> <property name="user" ref="user4"></property> <property name="maps"> <map> <entry key="key1" value="值1"></entry> <entry key="key2" value="值2"></entry> <!-- 引用<entry key="key3" value-ref="user1"></entry> --> </map> </property> </bean> </beans>
Main.java:
package com.shuoeasy.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); // bean引用 Order order = (Order) ctx.getBean("order"); System.out.println("bean引用->" + order); // 内部bean Order order2 = (Order) ctx.getBean("order2"); System.out.println("内部bean->" + order2); // 赋值为null Order order3 = (Order) ctx.getBean("order3"); System.out.println("赋值为null->" + order3); // 级联属性赋值 Order order4 = (Order) ctx.getBean("order4"); System.out.println("级联属性赋值->" + order4); // 集合 Order order5 = (Order) ctx.getBean("order5"); System.out.println("集合->" + order5); // map Order order6 = (Order) ctx.getBean("order6"); System.out.println("map->" + order6); } }
输出:
bean引用->Order [id=1234, user=User [id=0, name=老鹰开灰机], titles=null, maps=null]
内部bean->Order [id=9527, user=User [id=0, name=内部bean], titles=null, maps=null]
赋值为null->Order [id=5566, user=null, titles=null, maps=null]
级联属性赋值->Order [id=7788, user=User [id=123, name=说易事], titles=null, maps=null]
集合->Order [id=7788, user=User [id=123, name=说易事], titles=[标题1, 标题2], maps=null]
map->Order [id=7788, user=User [id=123, name=说易事], titles=null, maps={key1=值1, key2=值2}]