演示的spring版本为4.2.4
属性注入分为2大类,一个是使用配置文件,一个是使用注解annotation,这一篇主要使用配置文件演示。
配置文件进行注入,有构造方法注入,依赖对应的构造方法,普通属性注入、p名称空间注入、SpEl方法,这三个依赖对应的set方法。
1、构造方法演示
实体类,没有get、set方法,只有一个全参构造方法:
public class User implements Serializable { private static final long serialVersionUID = 1L; private String name; private Integer age; public User(String name, Integer age) { this.name = name; this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
配置:
<!-- 构造方法注入 --> <bean id="User" class="com.itheima.domain.User" > <constructor-arg name="name" value="郑爽"/> <constructor-arg name="age" value="16"/> </bean>
测试方法:
/** * 构造方法注入 */ @Test public void test1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User)ac.getBean("User"); System.out.println(user); }
结果:
2、普通set方法注入
实体类 ,将之前的User类引入,同时只设置了set方法:
public class Phone implements Serializable { private static final long serialVersionUID = 1L; private String brand; private Double price; private User user; public void setUser(User user) { this.user = user; } @Override public String toString() { return "Phone{" + "brand='" + brand + '\'' + ", price=" + price + ", user=" + user + '}'; } public void setBrand(String brand) { this.brand = brand; } public void setPrice(Double price) { this.price = price; } }
配置(引用类型使用ref属性,引入对应的id):
<bean id="Phone" class="com.itheima.domain.Phone"> <property name="brand" value="华为-mate10"></property> <property name="price" value="4100"></property> <property name="user" ref="User"></property> </bean>
测试方法:
/** * 普通set方法注入 */ @Test public void test2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Phone phone = (Phone)ac.getBean("Phone"); System.out.println(phone); }
结果:
3、使用p名称空间的方法,需要引入名称空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" >
实体类跟之前一样,修改下配置文件:
<!-- 引入p名称空间注入 需要导入:xmlns:p="http://www.springframework.org/schema/p"--> <bean id="Phone1" class="com.itheima.domain.Phone" p:brand="OPPO" p:price="3888" p:user-ref="User"></bean>
测试方法:
/** * p名称空间注入 */ @Test public void test3(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Phone phone = (Phone)ac.getBean("Phone1"); System.out.println(phone); }
结果:
4、使用SpEl方法注入
实体类跟之前一样,修改下配置文件:
<!-- 使用SpEl表达式注入 --> <bean id="Phone2" class="com.itheima.domain.Phone" > <property name="brand" value="#{'苹果'}"></property> <property name="price" value="#{8888}"></property> <property name="user" value="#{User}"></property> </bean>
测试方法
/** * 使用SpEl表达式注入 */ @Test public void test4(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Phone phone = (Phone)ac.getBean("Phone2"); System.out.println(phone); }
结果:
下面讲的是,封装的属性是数组,集合怎么注入:
实体类:
public class ListDemo implements Serializable { private static final long serialVersionUID = 1L; private List<String> list; private Set<String> set; private Map<String,String> map; private Properties properties; private String[] names; @Override public String toString() { return "ListDemo{" + "list=" + list +System.lineSeparator()+ ", set=" + set +System.lineSeparator()+ ", map=" + map +System.lineSeparator()+ ", properties=" + properties +System.lineSeparator()+ ", names=" + Arrays.toString(names) + '}'; } public void setProperties(Properties properties) { this.properties = properties; } public void setNames(String[] names) { this.names = names; } public void setMap(Map<String, String> map) { this.map = map; } public void setSet(Set<String> set) { this.set = set; } public void setList(List<String> list) { this.list = list; } }
配置文件:
<!-- 集合的属性注入 --> <bean id="ListDemo" class="com.itheima.domain.ListDemo"> <property name="list"> <list> <value>茭白</value> <value>莲藕</value> <value>水芹</value> <value>芡实(鸡头米)</value> <value>茨菰(慈菇)</value> <value>荸荠</value> <value>莼菜</value> <value>菱</value> </list> </property> <property name="set"> <set> <value>君问归期未有期</value> <value>巴山夜雨涨秋池</value> <value>何当共剪西窗烛</value> <value>却话巴山夜雨时</value> </set> </property> <property name="map"> <map> <entry key="李白" value="《将进酒》"></entry> <entry key="杜甫" value="《茅屋为秋风所破歌》"></entry> <entry key="李商隐" value="《无题》"></entry> <entry key="杜牧" value="《泊秦淮》"></entry> </map> </property> <property name="names"> <array> <value>《K歌之王》</value> <value>《十年》</value> <value>《浮夸》</value> <value>《幸福摩天轮》</value> <value>《稳稳的幸福》</value> </array> </property> <property name="properties"> <props> <prop key="李居丽">TIAMO</prop> <prop key="咸恩静">Sugar Free</prop> <prop key="朴孝敏">NO.9</prop> <prop key="朴智妍">Sexy Love</prop> <prop key="朴素妍">Day By Day</prop> <prop key="全宝蓝">Roly Poly</prop> <prop key="T-ara">cry cry</prop> </props> </property> </bean>
测试方法:
/** * 集合的属性注入 */ @Test public void test5(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); ListDemo listDemo = (ListDemo)ac.getBean("ListDemo"); System.out.println(listDemo); }
结果: