一.spring Ioc容器补充(1) Spring Ioc容器 DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入field(注解)/接口注入out 装配的方式:手动装配<property>,<constructor-arg>,@Autowired,@Resource/自动装配 1.装配各种类型的属性: A、简单属性value属性或者value元素 <bean id="person" class="com.lspring.step2.Person"> <property name="name" value="liyy"></property> <!-- property元素中的内容叫属性值,会自动把value描述的值转换成对应属性的类型 --> <property name="age" > <value>30</value> </property> <property name="tel"><value>ABCD</value></property> <!-- 也可以使用value装配一些Spring支持的类型 --> <property name="homePage"><value>http://google.com</value></property> </bean> B、引用其它bean,使用ref属性或者ref标签 <!--ref引用其它的bean,local表示引用本容器中的bean,parent表示引用父容器中的某个bean,bean表示先在当前容器中找,找不到再到父容器中找 --> <property name="parent"> <ref bean="person_c"></ref> </property> C、使用内部bean <property name="parent"> <bean class="com.lspring.step2.Person"> <constructor-arg value="Jerry"></constructor-arg> <constructor-arg> <value>33</value> </constructor-arg> </bean> </property> D、使用集合/装配集合 1).数组 代码: private String[] favs; public String[] getFavs() { return favs; } public void setFavs(String[] favs) { this.favs = favs; } xml配置: <!-- 装配数组 --> <property name="favs"> <array> <value>足球</value> <value>篮球</value> <value>排球</value> </array> </property> 2).装配list private List<String> school; public List<String> getSchool() { return school; } public void setSchool(List<String> school) { this.school = school; } <!-- 装配list集合 --> <property name="school"> <list> <value>中华第一小学</value> <value>中华第一高中</value> <value>中华第一大学</value> </list> </property> 3).装配set 不接受重复的值,只显示一条重复的传下 private Set<String> cities; public Set<String> getCities() { return cities; } public void setCities(Set<String> cities) { this.cities = cities; } <property name="cities"> <set> <value>shanghai</value> <value>shanghai</value> <value>shanghai</value> </set> </property> E.装配Map private Map<String, Double> scores; public Map<String, Double> getScores() { return scores; } public void setScores(Map<String, Double> scores) { this.scores = scores; } <!-- 装配Map --> <property name="scores"> <map> <entry key="语文" value="50"></entry> <entry key="外文" value="30"></entry> <!-- 也可以使用key-ref,和value-ref来指向其他的bean --> </map> </property> F.装配properties private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } <!-- 装配属性类型 --> <!-- <property name="properties"> <props> <prop key="qq">1213343</prop> <prop key="msn">kkk@qqq.com</prop> </props> </property> --> <!-- 在value中直接使用值对作为属性内容 --> <property name="properties"> <value> qq=133234 msn=1312@dfjk.com </value> </property> G.装配空值 private Integer age=25; 默认给age赋初值 <bean id="person_null" class="com.lspring.step2.Person"> <property name="name" value="小哥李"></property> <property name="age"> <!-- 使用nul标签来指定空值。 --> <null></null> </property> </bean>
代码如下:Person.java PersonDao.java
package com.lspring.step2; import java.net.URL; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Vector; public class Person { private String name; private Integer age=25; private String tel; private Person parent; private String[] favs; private List<String> school = new Vector(); private Set<String> cities; private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } private Map<String, Double> scores; public Map<String, Double> getScores() { return scores; } public void setScores(Map<String, Double> scores) { this.scores = scores; } public Set<String> getCities() { return cities; } public void setCities(Set<String> cities) { this.cities = cities; } public List<String> getSchool() { return school; } public void setSchool(List<String> school) { this.school = school; } public String[] getFavs() { return favs; } public void setFavs(String[] favs) { this.favs = favs; } private URL homePage; public URL getHomePage() { return homePage; } public void setHomePage(URL homePage) { this.homePage = homePage; } public String getName() { return name; } public Person(String name, Integer age) { super(); this.name = name; this.age = age; } public Person() { super(); System.out.println("初始化person!"); } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public Person getParent() { return parent; } public void setParent(Person parent) { this.parent = parent; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", tel=" + tel + ", parent=" + parent + ", favs=" + Arrays.toString(favs) + ", school=" + school + ", cities=" + cities + ", properties=" + properties + ", scores=" + scores + ", homePage=" + homePage + "]"; } }
package com.lspring.step2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class PersonDao { @Test public void injectTest(){ @SuppressWarnings("resource") ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans_step2.xml"); System.out.println(applicationContext.getBean("person")); System.out.println(applicationContext.getBean("person_c")); Person person = (Person) applicationContext.getBean("person"); //Spring 会把集合类型,初始化时统一使用某种类型 System.out.println(person.getSchool().getClass()); System.out.println("person_null:"+applicationContext.getBean("person_null")); } }
xml文件如下:beans_step2.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-3.2.xsd"> <bean id="person_c" class="com.lspring.step2.Person"> <constructor-arg> <value>Tom</value> </constructor-arg> <constructor-arg value="22"></constructor-arg> <property name="parent"> <bean class="com.lspring.step2.Person"> <constructor-arg value="Jerry"></constructor-arg> <constructor-arg> <value>33</value> </constructor-arg> </bean> </property> </bean> <bean id="person" class="com.lspring.step2.Person"> <property name="name" value="liyy"></property> <!-- property元素中的内容叫属性值,会自动把value描述的值转换成对应属性的类型 --> <property name="age"> <value>30</value> </property> <property name="tel"> <value>ABCD</value> </property> <!-- 也可以使用value装配一些Spring支持的类型 --> <property name="homePage"> <value>http://google.com</value> </property> <!-- 1. <property name="parent" ref="person_c"></property>=2. <property name="parent"><ref bean="person_c"/></property> --> <!--ref引用其它的bean,local表示引用本容器中的bean,parent表示引用父容器中的某个bean,bean表示先在当前容器中找,找不到再到父容器中找 --> <property name="parent"> <ref bean="person_c"></ref> </property> <!-- 装配数组 --> <property name="favs"> <array> <value>足球</value> <value>篮球</value> <value>排球</value> </array> </property> <!-- 装配list集合 --> <property name="school"> <list> <value>中华第一小学</value> <value>中华第一高中</value> <value>中华第一大学</value> </list> </property> <!-- 装配set集合 --> <property name="cities"> <set> <value>shanghai</value> <value>shanghai</value> <value>shanghai</value> </set> </property> <!-- 装配Map --> <property name="scores"> <map> <entry key="语文" value="50"></entry> <entry key="外文" value="30"></entry> <!-- 也可以使用key-ref,和value-ref来指向其他的bean --> </map> </property> <!-- 装配属性类型 --> <!-- <property name="properties"> <props> <prop key="qq">1213343</prop> <prop key="msn">kkk@qqq.com</prop> </props> </property> --> <!-- 在value中直接使用值对作为属性内容 --> <property name="properties"> <value> qq=133234 msn=1312@dfjk.com </value> </property> </bean> <bean id="person_null" class="com.lspring.step2.Person"> <property name="name" value="小哥李"></property> <property name="age"> <!-- 使用nul标签来指定空值。 --> <null></null> </property> </bean> </beans>