• Spring_3.1


    出处:http://www.cnblogs.com/JsonShare 

    Spring学习(11)---JSR-250标准注解之 @Resource、@PostConstruct、@PreDestroy

     

    1)@Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解)

        Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。 

        @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);如果没有匹配,则回退为一个原始类型(InjectionDAO)进行匹配,如果匹配则自动装配;

    2)@PostConstruct 、@PreDestroy

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

          使用@PostConstruct@PreDestroy 注释可以指定多个初始化 / 销毁方法,那些被标注@PostConstruct  或 @PreDestroy 注释的方法都会在初始化 / 销毁时被执行。

    1

    2

    3

    4

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package com.beanannotation.jsr;
     
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
     
    import org.springframework.stereotype.Service;
     
    @Service
    public class JsrService {
         
        @PostConstruct
        public void init(){
            System.out.println("JsrService init.");
        }
         
        @PreDestroy
        public void destory(){
            System.out.println("JsrService destory.");
        }
     
    }

    实例:

     定义两个类JsrDAO、JsrService

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package com.beanannotation.jsr;
     
    import org.springframework.stereotype.Repository;
     
    @Repository
    public class JsrDAO {
     
        public void save(String s){
            System.out.println("操作数据库保存数据:"+s);
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package com.beanannotation.jsr;
     
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
     
    import org.springframework.stereotype.Service;
     
    @Service
    public class JsrService {
     
    // @Resource方式1
    // @Inject方式3
     private JsrDAO jsrDAO;
     
    // @Resource方式2
     @Inject//方式4
     public void setJsrDAO(@Named("jsrDAO") JsrDAO jsrDAO) {
      this.jsrDAO = jsrDAO;
     }
     
     
        @PostConstruct
        public void init(){
            System.out.println("JsrService init.");
        }
         
        @PreDestroy
        public void destory(){
            System.out.println("JsrService destory.");
        }
     
        public void save(String arg){
            jsrDAO.save(arg);
        }
    }

     XML配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?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-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd">
             
            <context:component-scan base-package="com.beanannotation.jsr">
            </context:component-scan>   
    </beans>

     单元测试:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package com.beanannotation.jsr;
     
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    @RunWith(BlockJUnit4ClassRunner.class)
    public class UnitTest {
     
        @Test
        public void test(){
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beanannotation.xml");
            JsrService service = (JsrService)context.getBean("jsrService");
            service.save("data");
            context.close();     // 关闭 Spring 容器,以触发 Bean 销毁方法的执行
     
        }
         
    }

     结果:

    复制代码
    2015-7-7 13:05:43 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ed1e89e: startup date [Tue Jul 07 13:05:43 CST 2015]; root of context hierarchy
    2015-7-7 13:05:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]
    JsrService init.
    2015-7-7 13:05:44 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4ed1e89e: startup date [Tue Jul 07 13:05:43 CST 2015]; root of context hierarchy
    操作数据库保存数据:data
    JsrService destory.
    复制代码

     Spring学习(12)--- @Autowired与@Resource 对比

     

    Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource、 @PostConstruct及@PreDestroy。

    1. @Autowired   

    @Autowired是Spring 提供的,需导入Package:  org.springframework.beans.factory.annotation.Autowired;   

    只按照byType 注入。

    2. @Resource   

    @Resource默认按 byName 自动注入,是J2EE提供的, 需导入Package:  javax.annotation.Resource;   

    @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方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;     

    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入。   

    3. 使用区别   

    1
    2
    3
    4
    5
    6
    @Resource(name="loginService")   
    private LoginService loginService;     
    //---------------------------------
    @Autowired(required=false)    
    @Qualifier("loginService")    
    private LoginService loginService;

    (1) @Autowired 与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上;   

    (2) @Autowired 默认按类型装配,默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false).如果我们想使用名称装配可以结合 @Qualifier注解进行使用;   

    (3) @Resource(这个注解属于J2EE的),默认安装名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。     

    推荐使用@Resource注解在字段上,这样就不用写setter方法了.并且这个注解是属于J2EE的,减少了与Spring的耦合,这样代码看起就比较优雅 。

    另外,通过实践,还总结出一条规律:

    如果将@requied或者@autowired写了set方法之上,则程序会走到set方法内部。

    但如果写在了field之上,则不会进入set方法当中。

                                

  • 相关阅读:
    SPOJ 8222 NSUBSTR
    bzoj千题计划284:bzoj2882: 工艺
    bzoj千题计划283:bzoj4516: [Sdoi2016]生成魔咒(后缀数组)
    bzoj千题计划282:bzoj4517: [Sdoi2016]排列计数
    bzoj千题计划281:bzoj4558: [JLoi2016]方
    bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
    bzoj千题计划279:bzoj4591: [Shoi2015]超能粒子炮·改
    bzoj千题计划278:bzoj4590: [Shoi2015]自动刷题机
    bzoj千题计划277:bzoj4513: [Sdoi2016]储能表
    bzoj千题计划276:bzoj4515: [Sdoi2016]游戏
  • 原文地址:https://www.cnblogs.com/charles999/p/6644174.html
Copyright © 2020-2023  润新知