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 注释的方法都会在初始化 / 销毁时被执行。

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

package com.beanannotation.jsr;

import org.springframework.stereotype.Repository;

@Repository
public class JsrDAO { public void save(String s){
System.out.println("操作数据库保存数据:"+s);
}
}
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
private 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配置:

<?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>

单元测试:

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学习(11)---JSR-250标准注解之 @Resource、@PostConstruct、@PreDestroy的更多相关文章

  1. Spring IOC之 使用JSR 330标准注解

    从Spring 3.0开始,Spring提供了对 JSR 330标准注解的支持.这些注解可以喝Spring注解一样被扫描到.你只需要将相关的Jar包加入到你的classpath中即可. 注意:如果你使 ...

  2. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  3. Spring学习笔记(14)——注解零配置

    我们在以前学习  Spring  的时候,其所有的配置信息都写在  applicationContext.xml  里,大致示例如下: java代码: <beans> <bean n ...

  4. Spring5参考指南:JSR 330标准注解

    文章目录 @Inject 和 @Named @Named 和 @ManagedBean 之前的文章我们有讲过,从Spring3.0之后,除了Spring自带的注解,我们也可以使用JSR330的标准注解 ...

  5. spring学习(四)使用注解代替xml配置

    用的是IDEA的maven工程,pom.xml文件导包依赖省略 一.书写要导入容器的实体类 import org.springframework.beans.factory.annotation.Va ...

  6. Spring系列12: `@Value` `@Resource` `@PostConstruct` `@PreDestroy` 详解

    本文内容 @Resource实现依赖注入 @Value详细使用 @PostConstruct @PreDestroy的使用 @Resource实现依赖注入 前面章节介绍了使用@Autowired注入依 ...

  7. Spring学习(10)--- @Qualifier注解

    按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数 可用于注解集合类型变量 例子: package c ...

  8. Spring学习8-Spring事务管理(注解式声明事务管理)

    步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...

  9. Spring学习(9)--- @Autowired注解(二)

    可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,Applicat ...

随机推荐

  1. System.map详解

    system.map内容格式为:线性地址类型符号 具体内容如下: 00100000 A phys_startup_32 c0100000 T startup_32 c0100000 A _text   ...

  2. PHP 学习笔记(4)

    声明类属性或方法为静态,就可以不实例化类而直接访问.静态属性不能通过一个类已实例化的对象来访问(但静态方法可以). PHP 5 支持抽象类和抽象方法.定义为抽象的类不能被实例化 使用接口(interf ...

  3. #include<> 和#include“”的区别

    1.< >引用的是编译器的类库路径里面的头文件2."    "引用的是程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继续在类库路径里搜寻该头文件 ...

  4. 【DP系列学习一】简单题:kickstart2017 B.vote

    https://code.google.com/codejam/contest/6304486/dashboard#s=p1 这是一道简单的dp,dp[i][j]代表A的voter为i,B的voter ...

  5. Linux命令之初出茅庐

    此处讲解常用到的参数选项: ls 是列出文件的意思 ls -a ,查看所有文件包含隐藏文件 ls -l ,查看与文件相关的所有属性信息 ls -i ,查看文件的inode信息 ls -h,按照更为容易 ...

  6. MongoDB副本集的常用操作及原理

    本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操 ...

  7. C#基础知识-数据类型(一)

    俗话说温故而知新,学习一门知识最好的方法就是不断的去咀嚼回味,学习编程更是如此.对于.NET平台中的C#语言而言,有着强大的类库.不断的在更新迭代几乎每隔一年都会更新一个新的模块,.NET Framw ...

  8. 1005 Number Sequence

    Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...

  9. Activity设置全屏显示的两种方式及系统自带theme属性解析

    转载说明:原贴地址:http://blog.csdn.net/a_running_wolf/article/details/50480386 设置Activity隐藏标题栏.设置Activity全屏显 ...

  10. 瀑布流原生ajax,demo

    最近听朋友们说起瀑布流挺多的,自己就去研究下了,一个简单的原生demo,分享给大家... 简单分为三个文档,有详细的注释 img:ajax.php:demo.php 其中img中放入图片 1.jpg: ...