zoukankan      html  css  js  c++  java
  • Spring 学习笔记(八)—— 注解使用整合

     @Autowired  —— 自动装配

    需先在配置文件中,配置一个org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor的Bean。

    <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
        <bean class="org.springframework.beans.factory.annotation.
            AutowiredAnnotationBeanPostProcessor"/>

    主要作用与Setter/构造函数前:

    public class Boss {
        private Car car;
        private Office office;
    
         @Autowired
        public void setCar(Car car) {
            this.car = car;
        }
     
        @Autowired
        public void setOffice(Office office) {
            this.office = office;
        }
        …
    }
    public class Boss {
        private Car car;
        private Office office;
     
        @Autowired
        public Boss(Car car ,Office office){
            this.car = car;
            this.office = office ;
        }
     
        …
    }

     

    @Qualifier —— 指定注入Bean的名称

      当同一个Class申明了两个Bean时,在装配时用作区分。

      @Qualifier 只能和 @Autowired 结合使用,是对 @Autowired 有益的补充。一般来讲,@Qualifier 对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。

      例如:Office有office和office1两个Bean。

    public class Boss {
        private Car car;
        private Office office;
    
        @Autowired
        public Boss(Car car , @Qualifier("office")Office office){
            this.car = car;
            this.office = office ;
        }
    }

    @Resource —— 自动装配(2)

    @Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面@Resource 默认按 byName 自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将@Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。

    Resource 注释类位于 Spring 发布包的 lib/j2ee/common-annotations.jar 类包中,因此在使用之前必须将其加入到项目的类库中。来看一个使用@Resource 的例子:

    public class Boss {
        // 自动注入类型为 Car 的 Bean
        @Resource
        private Car car;
    
        // 自动注入 bean 名称为 office 的 Bean
        @Resource(name = "office")
        private Office office;
    }

    @PostConstruct 和 @PreDestory

    Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,您既可以通过实现 InitializingBean/DisposableBean 接口来定制初始化之后 / 销毁之前的操作方法,也可以通过 <bean> 元素的 init-method/destroy-method 属性指定初始化之后 / 销毁之前调用的操作方法。关于 Spring 的生命周期,笔者在《精通 Spring 2.x—企业应用开发精解》第 3 章进行了详细的描述,有兴趣的读者可以查阅。

    JSR-250 为初始化之后/销毁之前方法的指定定义了两个注释类,分别是 @PostConstruct 和 @PreDestroy,这两个注释只能应用于方法上。标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。

    public class Boss {
        @Resource
        private Car car;
    
        @Resource(name = "office")
        private Office office;
    
        @PostConstruct
        public void postConstruct1(){
            System.out.println("postConstruct1");
        }
    
        @PreDestroy
        public void preDestroy1(){
            System.out.println("preDestroy1"); 
        }
        …
    }

    您只需要在方法前标注 @PostConstruct@PreDestroy,这些方法就会在 Bean 初始化后或销毁之前被 Spring 容器执行了。

    我们知道,不管是通过实现 InitializingBean/DisposableBean 接口,还是通过 <bean> 元素的init-method/destroy-method 属性进行配置,都只能为 Bean 指定一个初始化 / 销毁的方法。但是使用 @PostConstruct@PreDestroy 注释却可以指定多个初始化 / 销毁方法,那些被标注 @PostConstruct@PreDestroy 注释的方法都会在初始化 / 销毁时被执行。

    @Component

    Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:

    • @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
    • @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
    • @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。

    通过在类上使用 @Repository、@Component、@Service 和 @Constroller 注解,Spring 会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring 受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与 @Repository 是完全相同的。

    import org.springframework.stereotype.Component;
    
    @Component
    public class Car {
        …
    }

    仅需要在类定义处,使用 @Component 注释就可以将一个类定义了 Spring 容器中的 Bean。

    @Scope —— 指定作用范围

    @Scope("prototype")
    @Component("boss")
    public class Boss {
        …
    }

    这样,当从 Spring 容器中获取 boss Bean 时,每次返回的都是新的实例了。

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


    感谢:http://blog.csdn.net/xyh820/article/details/7303330/

  • 相关阅读:
    VScode 修改中文字体
    missing KW_END at ')' near '<EOF>'
    SQL inner join, join, left join, right join, full outer join
    SQL字符替换函数translater, replace
    SQL COOKBOOK SQL经典实例代码 笔记第一章代码
    sqlcook sql经典实例 emp dept 创建语句
    dateutil 2.5.0 is the minimum required version python
    安装postgresql后找不到服务 postgresql service
    Postgres psql: 致命错误: 角色 "postgres" 不存在
    【西北师大-2108Java】第十六次作业成绩汇总
  • 原文地址:https://www.cnblogs.com/rekent/p/7411761.html
Copyright © 2011-2022 走看看