Spring学习(四) 注入依赖对象
依赖注入(Dependency Injection)
所谓的依赖注入就是指:在运行期间,由外部容器动态地将依赖对象注入到组件中。
一、注入方式
1、通过构造方法注入
package com.bill.impl;import com.bill.PersonService;import com.bill.dao.PersonDao;/** * @author Bill */public class PersonServiceBean implements PersonService {private PersonDao personDao;private String name;public PersonServiceBean(PersonDao personDao, String name) {this.personDao = personDao;this.name = name;}public void save(){personDao.add();System.out.println(name);}}<bean id="personDao" type="com.bill.dao.PersonDao" ref="personDao"></constructor-arg><constructor-arg index="1" value="good"></constructor-arg></bean>
AbstractApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");PersonService personService = (PersonService)act.getBean("personService");personService.save(); act.close();<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/><bean id="personDao" name="code">package com.bill.impl;import javax.annotation.Resource;import com.bill.PersonService;import com.bill.dao.PersonDao;/** * @author Bill */public class PersonServiceBean implements PersonService {@Resource private PersonDao personDao;//@Resource(name="personDao") private PersonDao personDao;//采用指定name属性的方式,寻找xml中指定的beanpublic void save(){personDao.add();}}package com.bill.impl;import javax.annotation.Resource;import com.bill.PersonService;import com.bill.dao.PersonDao;/** * @author Bill */public class PersonServiceBean implements PersonService {private PersonDao personDao;@Resourcepublic void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void save(){personDao.add();}}package com.bill.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import com.bill.PersonService;import com.bill.dao.PersonDao;/** * @author Bill */public class PersonServiceBean implements PersonService {//默认按类型装配@Autowired private PersonDao personDao;//使用@Qualifier("personDao")将@Autowired装配方式改为按名称装配//@Autowired @Qualifier("personDao") private PersonDao personDao;public void save(){personDao.add();}}public class PersonServiceBean implements PersonService {private String name;private Integer id;private Set<String> sets = new HashSet<String>();private Map<String, String> maps = new HashMap<String, String>();private Properties properties = new Properties();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}public Map<String, String> getMaps() {return maps;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public Set<String> getSets() {return sets;}public void setSets(Set<String> sets) {this.sets = sets;}}<bean id="personService" value="bill"/><property name="id" value="88"/><property name="sets"><set><value>set1</value><value>set2</value><value>set3</value></set></property><property name="maps"><map><entry key="key1" value="value1"></entry><entry key="key2" value="value2"></entry><entry key="key3" value="value3"></entry></map></property><property name="properties"><props><prop key="key-1">propValue1</prop><prop key="key-2">propValue2</prop><prop key="key-3">propValue3</prop></props></property></bean>
System.out.println("===========set============");for(String value : personService.getSets()){System.out.println(value);}System.out.println("===========map============");for(String key : personService.getMaps().keySet()){System.out.println(key + "=" + personService.getMaps().get(key));}System.out.println("===========properties========");for(Object propsKey : personService.getProperties().keySet()){System.out.println(propsKey + "=" + personService.getProperties().getProperty((String) propsKey));}<bean id="personDao" ref="personDao"></property></bean>
<bean id="personService" com.bill.dao.impl.PersonDaoBean"/></property></bean>