Spring @Resource、@Autowired、@Qualifier的注解注入及区别

来源:互联网 发布:centos lnmp安装包 编辑:程序博客网 时间:2024/06/05 23:46

spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖。在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入。虽然@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区别的。首先来看一下:

区别


a。@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入; 
b。@Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用; 
c。@Resource注解是又J2EE提供,而@Autowired是由spring提供,故减少系统对spring的依赖建议使用 @Resource的方式; 
d。 @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上 

配置信息

使用注解的方式,我们需要修改spring配置文件的头信息,一般都是appcontext-*.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"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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config/></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注解

修改以上配置文件的头信息后,我们就可以在Java代码通过注解方式来注入bean,看下面代码

@Resource

public class StudentService3 implements IStudentService {//@Resource(name="studentDao")放在此处也是可行的private IStudentDao studentDao;private String id;public void setId(String id) {this.id = id;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

@Resource(name="studentDao")// 通过此注解完成从spring配置文件中查找名称为studentDao的bean来装配字段studentDao,如果spring配置文件中不存在 studentDao名称的bean则转向按照bean类型经行查找

public void setStudentDao(IStudentDao studentDao) {this.studentDao = studentDao;}public void saveStudent() {studentDao.saveStudent();System.out.print(",ID 为:"+id);   }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

配置文件添加如下信息

<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean><bean id="studentService3" class="com.wch.service.impl.StudentService3" />
  • 1
  • 2
  • 1
  • 2

@Autowired

public class StudentService3 implements IStudentService { //@Autowired放在此处也是可行的private IStudentDao studentDao;private String id;public void setId(String id) {this.id = id;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

@Autowired//通过此注解完成从spring配置文件中 查找满足studentDao类型的bean

//@Qualifier("studentDao")则按照名称经行来查找转配的 public void setStudentDao(IStudentDao studentDao) {this.studentDao = studentDao;}public void saveStudent() {studentDao.saveStudent();System.out.print(",ID 为:"+id);}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

配置文件添加如下信息

<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean><bean id="studentService3" class="com.wch.service.impl.StudentService3" />
  • 1
  • 2
  • 1
  • 2

总结

在java代码中可以使用@Autowire或者@Resource注解方式进行装配,这两个注解的区别是: 
@Autowired 默认按照类型装配,默认情况下它要求依赖对象必须存在如果允许为null,可以设置它required属性为false,如果我们想使用按照名称装配,可以结合@Qualifier注解一起使用;

@Resource默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配,可以通过name属性指定,如果没有指定name属 性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找 依赖对象. 
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖的对象时候,会回退到按照类型装配,但一旦指定了name属性,就只能按照名称装配了.

转载:

http://blog.csdn.net/mccand1234/article/details/55800154


参考: 
http://blog.csdn.net/baple/article/details/17891755

阅读全文
0 0