spring注释总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wo3002807/article/details/48443357

一、 概述

     正因为有了注释的存在,可以节省了大量的工作在applicationContext.xml上的配置,交给容器自己来扫描检测。也正是因为注释,不用再配置文件上进行配置,一些重要的信息无法在配置上看到,这样也会导致配置体积过于庞大,无法维护。
     那么在配置文件上如何设置,才能让容器自己进行扫描检测bean,存储到容器当中呢?
<?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.xsd
                               http://www.springframework.org/schema/context
                             http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/><!--依赖注入-->
<context:component-scan base-package="xxx.xx.xx.xx"/><!--注册bean-->
</beans>
     说明:
     1. <context:annotation-config/> 的作用
     是隐式地向 Spring 容器注册如下四个Bean,这是注解,注入功能的驱动:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor 
     2. <context:annotation-config/>与<context:component-scan base-package="xxx.xx"/>的区别?
<context:annotation-config/>:只能够在已经注册(在配置上注册的bean)的bean上面其作用。
<context:component-scan base-package=""/>:除了拥有<context:annotation-config/>的功能外(如果使用后者,前者就没有必要使用了),还具有自动将带有@component,@sevice,@Respository等注解的对象注册到spring容器的功能。
     Note:具体参照《spring <context:annotation-config> 跟 <context:component-scan>诠释及区别》
     3. 如果想使用@Resource 、@PostConstruct、@PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。 
     4. 如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。  
     5. 如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
二、 注释
1. @Autowired
     自动装配,是根据类型进行自动装配。 默认是根据类型注入,可以用于构造器、字段、方法注入
     当同时存在多个bean时,需要@Qualifier(限定描述符)来指定: @Qualifier(value = "限定标识符") 
@Autowired     
public void setUserDao( @Qualifier("userDao")  UserDao userDao) {    
    this.userDao = userDao;    
}    
     当不存在bean时:
@Autowired( required = false)    
public void setUserDao(UserDao userDao) {    
    this.userDao = userDao;    
}    

2. @Resource
     自动装配,@Resource 的作用相当于 @Autowired。
     @Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 type 自动注入策略。
public class Boss {
    // 自动注入类型为 Car 的 Bean
    @Resource
    private Car car;

    // 自动注入 bean 名称为 office 的 Bean
    @Resource(name = "office")
    private Office office;
}
     Note:一般情况下,我们无需使用类似于 @Resource(type=Car.class) 的注释方式,因为 Bean 的类型信息可以通过 Java 反射从代码中获取。

3. @PostConstruct 和 @PreDestroy
     Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,您既可以通过 实现 InitializingBean/DisposableBean 接口来定制初始化之后 / 销毁之前的操作方法,也可以通过   <bean> 元素的 init-method/destroy-method 属性指定初始化之后 / 销毁之前调用的操作方法。
     JSR-250 为初始化之后/销毁之前方法的指定定义了两个注释类,分别是 @PostConstruct 和 @PreDestroy,这两个注释只能应用于方法上。 标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
     Note:
       不管是通过实现 InitializingBean/DisposableBean 接口,还是通过 <bean> 元素的 init-method/destroy-method 属性进行配置,都只能为 Bean 指定一个初始化 / 销毁的方法。但是使用 @PostConstruct 和 @PreDestroy 注释却可以指定多个初始化 / 销毁方法,那些被标注 @PostConstruct 或 @PreDestroy 注释的方法都会在初始化 / 销毁时被执行。

4. @Component,@Respository(持久层),@Service(业务层),@Controller(控制层)
     注册bean。
     @Component 有一个可选的入参,用于指定 Bean 的名称。 则默认的bean名字为这个类的类名首字母小写。在 Boss 中,我们就将 Bean 名称定义为“boss“,例子:@Component("beanName")
     一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标
     Note:建议少用,可以用@Respository(持久层),@Service(业务层),@Controller(控制层)。该三个注释的语法与@Component类似

5. @Scope
     定义Bean的作用范围。在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过。
@Scope("session")    
@Component()    
public class UserSessionBean implements Serializable {    
    ...    
}   
     Note:
可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,避免struts中Action的线程安全问题。spring 默认scope 是单例模式(scope="singleton"),这样只会创建一个Action对象,每次访问都是同一Action对象,数据不安全,struts2 是要求每次次访问都对应不同的Action,scope="prototype" 可以保证当有请求的时候都创建一个Action对象

     后续继续完善。。。。

猜你喜欢

转载自blog.csdn.net/wo3002807/article/details/48443357