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" class=" com.bill.dao.impl.PersonDaoBean"/>
<bean id="personService" class="com.bill.impl.PersonServiceBean">
<constructor-arg index="0" 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();

2、通过属性setter方法注入
如二.1

3、使用注解方式注入
在java代码中使用@Autowired或者@Resource注解方式进行装配。这两个注解的区别是:@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。需配置xml文件如下:

<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" class=" com.bill.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="com.bill.impl.PersonServiceBean"></bean>
</beans>

这种配置隐式注册了多个对注解解析处理的处理器AutowriedAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor

使用@Resource注解的业务代码如下:
1)标注在字段上的方式

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中指定的bean
public void save(){
personDao.add();
}
}

2)标注在setter方法上的方式

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;

@Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}


public void save(){
personDao.add();
}
}

在@Resource中没有指定name属性时,Spring会根据该字段的名称personDao去xml中寻找匹配的bean,假如找不到,再去根据该字段的类型去寻找匹配的bean。

注意:@Resource注解在Spring安装目录的lib\j2ee\common-annotations.jar

使用@Autowired注解的业务代码如下:

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();
}
}


二、注入类型
1、基本类型以及集合类型对象注入

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" class="com.bill.impl.PersonServiceBean">
<property name="name" 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));
}


2、注入其他bean类型
1)方式一

<bean id="personDao" class=" com.bill.dao.impl.PersonDaoBean"/>
<bean id="personService" class="com.bill.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>

2)方式二(使用内部bean的方式进行注入,但该bean不能被其他bean使用)

<bean id="personService" class="com.bill.impl.PersonServiceBean">
<property name="personDao">
<bean class=" com.bill.dao.impl.PersonDaoBean"/>
</property>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值