Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

一、Spring定义bean,@Component、@Repository、@Service 和 @Controller

  Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释

  在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了 @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,
在 spring的配置文件里面只需要加上<context:annotation-config/> 和<context:component-scan base-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。   配置自动扫描如下:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <context:component-scan base-package=”com.eric.spring”> </beans>
其中base-package为需要扫描的包(含所有子包),各个注解的理解如下:
  1. @Service用于标注业务层组件,,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,如果需要自己改名字则:@Service("你自己改的bean名")。   
  2. @Controller用于标注控制层组件(如struts中的action)
  3. @Repository用于标注数据访问组件,即DAO组件
  4. 而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Service 
public class VentorServiceImpl implements iVentorService { } @Repository
public class VentorDaoImpl implements iVentorDao { }
getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意):
@PostConstruct 
public void init() {  
}  
@PreDestroy 
public void destory() {  
} 

 

二、Spring bean 注解注入汇总

  spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖。bean注入可以使用@Autowired、@Resource来完成。但它们之间是有区别的。首先来看一下:

 a、@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;

 b、@Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;当候选 Bean 数目不为1时的应对方法,在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。

 c、@Resource注解是由J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用@Resource的方式;

 d、@Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上

 e、使用@Autowired时你的Bean必须以@Service或@Component注解才行

 

  注解与传统的编码方式实现的注入的不同用法需要注意下:例子,把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注入时不要new这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。

(1)@Resource示例:

public class StudentService3 implements IStudentService {

    //@Resource(name="studentDao")放在此处也是可行的
    private IStudentDao studentDao;

    private String id;

public void setId(String id) {
this.id = id;
}

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

}

配置文件添加如下信息

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

 @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
  @Resource装配顺序
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

(2)@Autowired示例1

public class StudentService3 implements IStudentService {

  //@Autowired放在此处也是可行的
    private IStudentDao studentDao;

    private String id;

    public void setId(String id) {
        this.id = id;
    }

     @Autowired//通过此注解完成从spring配置文件中 查找满足studentDao类型的bean
    //@Qualifier("studentDao")则按照名称经行来查找转配的
    public void setStudentDao(IStudentDao studentDao) {
        this.studentDao = studentDao;
    }

    public void saveStudent() {
        studentDao.saveStudent();
        System.out.print(",ID 为:"+id);
    }

}

配置文件添加如下信息

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

3、@Autowired示例2

@Service("OrganDaoIbatis ")
public class OrganDaoIbatis extends BaseDao implements IOrganDao {
 @Autowired(required=false)
 @Qualifier("sqlMapClient")
 private SqlMapClient sqlClient = null;

使用 @Autowired(required = false)作用:

当候选 Bean 数目不为 1 时的应对方法

在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。

当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。

@Qualifier 注释指定注入 Bean 的名称 作用:

和找不到一个类型匹配 Bean 相反的一个错误是:如果 Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出BeanCreationException 异常。

使用Autowired时你的OrganDaoIbatis 必须以@Service或@Component注解才行。

在接口前面标上@Autowired和@Qualifier注释使得接口可以被容器注入,当接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入,如:

@Autowired     
@Qualifier("chinese")      
private Man man;

否则可以省略,只写@Autowired。 

 

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

@Resource默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配,可以通过name属性指定,如果没有指定name属 性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找 依赖对象.

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

三、Spring的@Required注解 检查属性

Spring 配置文件中 dependency-check 依赖检查的灵活性不够,并不能满足我们所有的需求

Spring还提供一种更加灵活的检查方式 

@Required注解检查 但他只检查属性是否已经设置而不会测试属性是否非空

 

下面我们看一下如何使用Spring提供的此机制

首先我们需要在程序里面加上注解

@Required
public void setProduct(Product product) {
   this.product = product;
}

注意@Required只能设置在setter方法上
接着我们需要在 配置文件中加上这样一句话

<bean class="org.springframework.beans.factory.annotation. 
RequiredAnnotationBeanPostProcessor"/>

这是Spring的一个处理器用于检查
如果Spring的版本是2.5或以上,我们可以直接用下列方式来配置

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>

这是简写形式

这样就配置完成了,这样如果任何带有@Required的属性未设置的话 将会抛出BeanInitializationException异常

 

posted on 2014-06-18 09:38  duanxz  阅读(3026)  评论(0编辑  收藏  举报