`

知识点四:依赖注入-使用Field注入(用于注解方式)

阅读更多
注入依赖对象可以采用手工装配和自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见装配结果

依赖注入――手工装配

手工装配依赖对象,有两种编程方式:基于xml和注解方式
1 在xml配置文件中,通过在bean节点下配置,如:
<bean id="personDao" class="cn.itcast.service.PersonDaoBean">
         <constructor-arg index="0" type="java.lang.String"
value="xxx"/>//使用构造器注入
         <property name="name" value="探索"/>//使用setter方法注入
</bean>

2 在代码里使用注解方式

在java代码里使用@Resource和@AutoWired的注解方式进行装配。但我们需要在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>

其中<context:annotation-config/> 这个配置隐式注册了多个对注释进行解析处理的处理器
比如:对@AuotWired :AutowireAnnotationBeanPostProcessor
        CommonAnnotationBeanPostProcessor
        PersistAnnotationBeanPostProcessor
        RequiredAnnotationBeanPostProcessor
注:@Resource注解 在spring的安装目录lib/j2ee/comm-annotation.jar

在java代码中使用@Autowired或者@Resource注解方式进行装配,这两个注解的区别是:@Autowired默认按照类型装配(这个是spring提供的),@Resource默认按照名称装配(这个是jdk1.6提供的),当找不到名称匹配的bean才会按照类型装配。『通常建议使用@Resource,因为这样不会与spring框架耦合』

@Autowired
Private PersonDao personDao://用于字段上
...
@Autowired
Public void setOrderDao(OrderDao orderDao){//用于属性的setter方法上
    This.orderDao = orderDao;
}


@Autowired注解是按照类型装配依赖对象的,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier(“personDaoBean”)
Private PersonDao personDao;


@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称做为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名称做为bean名称寻找依赖对象。
@Resource(name=”personDaoBean”)
Private PersonDao personDao;//用于字段上


::注意:: 如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时,@Resource注解会回退到按照类型装配。但一旦指定了name属性,就只能按照名称装配了。

import javax.annotation.Resource;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {	
	@Resource private PersonDao personDao;
	private String name;	
	public PersonServiceBean() {}
	public void save()
	{
		System.out.println("�ɹ�ִ��save����");
	}
}


<bean id="personDao" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean" class="cn.itcast.service.impl.PersonServiceBean"/>

这样,@Resource private PersonDao personDao;就会根据personDao到配置文件找到id/name为personDao的对象进行注入。如果没有找到会按照PersonDao类型进行查找进行注入。(因为@Resource默认按照名称查找,如果找不到按照类型查找)
注意1:如果改为<bean id="personDaoxxx" class="cn.itcast.dao.PersonDao"/>也是可以找到的,会按照类型查找!
注意2:如果改为
@Resource(name=”personDaoxxx”) private PersonDao personDao;也是可以的,但是这样如果在容器中没有名字为personDaoxxx的就不会按照类型去找了,就会出现异常!

上面是对字段进行注入,下面根据setter进行注入
import javax.annotation.Resource;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {	
	private PersonDao personDao;
	private String name;	
	@Resource
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}	
	public PersonServiceBean() {
	}
	public void save()
	{
		System.out.println("�ɹ�ִ��save����");
	}
}


<bean id="personDao" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean"/>

这样也是OK的。

@Resource是j2ee里面的应用,下面看一下spring的注解@Autowired

import org.springframework.beans.factory.annotation.Autowired;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {
	@Autowired
	private PersonDao personDao;
	private String name;
	public PersonServiceBean() {
	}
	public void save()
	{
		System.out.println("�ɹ�ִ��save����");
	}
}



<bean id="personDao" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean"/>

@Autowired默认是按照类型查找要注入的对象的。要改成按照名称查找注入可以这样:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {
	@Autowired @Qualifier("personDao")//按照指定名字注入
	private PersonDao personDao;
	private String name;
	public PersonServiceBean() {
	}
	public void save()
	{
		personDao.save();
	}
}


当然,如果@Autowired(required=true) @Qualifier("personDao")
private PersonDao personDao;

required=true :表示如果在配置文件中找不到指定的要注入的值,会抛出异常
required=false :表示如果在配置文件中找不到指定的要注入的值,赋一个null
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics