Spring基于Java的JSR-250注解
Spring还支持基于JSR-250的注解,其中包括@PostConstruct,@PreDestroy和@Resource注解。虽然这些注解不是真正需要的,因为你已经有其他替代品,但让我们简要了解一下。
@PostConstruct和@PreDestroy注解
要定义一个bean的设置和拆卸,我们只需使用init-method和/或destroy-method参数声明<bean> 。init-method属性指定一个在实例化后立即在bean上调用的方法。类似地,destroy-method指定在bean从容器中删除之前调用的方法。
在这里你可以使用@PostConstruct注解作为初始化回调和@PreDestroy注解的替代,作为销毁回调的替代。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testannotationjsr250</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testannotationjsr250</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies>
</project>
HelloWorld.java:
package com.jsoft.testspring.testannotationjsr250; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class HelloWorld {
private String messageString; public void setMessage(String message){
this.messageString = message;
} public void getMessage(){
System.out.println(this.messageString);
} @PostConstruct
public void initPost(){
System.out.println("@ init");
} @PreDestroy
public void destroyPre(){
System.out.println("@ destroy");
}
}
beans.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="helloWorld" class="com.jsoft.testspring.testannotationjsr250.HelloWorld">
<property name="Message" value="Hello World!"></property>
</bean> <context:annotation-config/> </beans>
App.java:
package com.jsoft.testspring.testannotationjsr250; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
AbstractApplicationContext abstractApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld helloWorld = (HelloWorld)abstractApplicationContext.getBean("helloWorld");
helloWorld.getMessage();
abstractApplicationContext.registerShutdownHook();//因为在AbstractApplicationContext类中才有registerShutdownHook()方法
}
}
测试结果:

@Resource注释
你可以对字段或setter方法使用@Resource注释,它与Java EE 5中的工作方式相同。@Resource注释采用“name”属性,将被解释为要注入的bean名称。
如果没有明确指定“name”,则默认名称是从字段名称或setter方法派生的。在一个字段的情况下,它需要字段名称,在setter方法的情况下,它将使用bean属性名称。
例子:
HelloWorld.java:
package com.jsoft.testspring.testannotationjsr250; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource; public class HelloWorld {
private String messageString; @Resource(name="msg")
public void setMessage(String message){
this.messageString = message;
} public void getMessage(){
System.out.println(this.messageString);
} @PostConstruct
public void initPost(){
System.out.println("@ init");
} @PreDestroy
public void destroyPre(){
System.out.println("@ destroy");
}
}
beans.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="msg" class="java.lang.String">
<constructor-arg index="0" value="Hello World!"></constructor-arg>
</bean> <bean id="helloWorld" class="com.jsoft.testspring.testannotationjsr250.HelloWorld"> </bean> <context:annotation-config/> </beans>
注意:当你使用@Resource注解时,此name指定的是一个bean,也就是一个类才能生效,而不是这个bean上的具体setter方法。所以这里直接用bean生成了一个String的类,然后在类的实例化传入有参的构造函数中。
测试结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test12/testannotationjsr250
Spring基于Java的JSR-250注解的更多相关文章
- Spring 基于Java的Bean声明
Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...
- Spring基于Java的配置
以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...
- Spring 基于 Java 的配置
前面已经学习如何使用 XML 配置文件来配置 Spring bean. 基于 Java 的配置可以达到基于XML配置的相同效果. 基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大 ...
- Spring学习笔记--使用Spring基于Java的配置
我们需要使用@Component注解来定义一个配置类,在配置类中我们定义Bean: package com.moonlit.myspring; import org.springframework.c ...
- spring实战六之使用基于java配置的Spring
之前接触的都是基于XML配置的Spring,Spring3.0开始可以几乎不使用XML而使用纯粹的java代码来配置Spring应用.使用基于java配置的Spring的步骤如下: 1. 创建基于ja ...
- [译]17-spring基于java代码的配置元数据
spring还支持基于java代码的配置元数据.不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下. spring基于java代码的配置元数据,可以通过@Configuration注解 ...
- spring源码解析:元注解功能的实现
前言 众所周知,spring 从 2.5 版本以后开始支持使用注解代替繁琐的 xml 配置,到了 springboot 更是全面拥抱了注解式配置.平时在使用的时候,点开一些常见的等注解,会发现往往在一 ...
- Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解
基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...
- Spring @Bean注解 (基于java的容器注解)
基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...
随机推荐
- js toString() 方法 Number() 方法 等 类型转换
1.1 数字类型转字符串 String() 变量.toString() toString() 方法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 1.2 字符串转数字类型 Num ...
- 阿里P7/P8学习路线图——技术封神之路
一.基础篇 JVM JVM内存结构 堆.栈.方法区.直接内存.堆和栈区别 Java内存模型 内存可见性.重排序.顺序一致性.volatile.锁.final 垃圾回收 内存分配策略.垃圾收集器(G1) ...
- django URL,views,html请求顺序
进来的请求转入/hello/. Django通过在ROOT_URLCONF配置来决定根URLconf. Django在URLconf中的所有URL模式中,查找第一个匹配/hello/的条目 ...
- JavaSE-02 变量 数据类型和运算符
学习要点 掌握变量的概念 掌握常用数据类型 掌握赋值运算符.算术运算符 掌握boolean数据类型和关系运算符 掌握变量的概念 面向过程程序的定义 程序的定义:程序=数据+算法+文档 程序要操作的数据 ...
- JS常用字符串处理方法应用总结
这篇文章主要总结了JS常用字符串的处理方法,需要的朋友可以参考下 1.indexOf()方法,从前往后查找字符串位置,大小写敏感,从0开始计数.同理,lastIndexOf() 方法从后往前,两个 ...
- 任务二:零基础HTML及CSS编码(一)
面向人群: 零基础或初学者 难度: 简单 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学习难度的合理性,但即使如此,真正决定课 ...
- C字符串指针遇到的问题
看下面的示例代码: int main() { char *ptr = "GeeksQuiz"; printf("%c\n", *&*&*ptr) ...
- 【转】Unable to load native-hadoop library for your platform(已解决)
1.增加调试信息寻找问题 2.两种方式解决unable to load native-hadoop library for you platform 附:libc/glibc/glib简介 参考: 1 ...
- 【HDU 6153】A Secret (KMP)
Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...
- robotframework使用requestsLibrary进行接口测试
一.定义 接口测试:接口测试通常是系统之间交互的接口,或者某个系统对外提供的一些接口服务 分类:RESTful.webservice接口 二.安装 进入C:\Pyhon27\scripts 先要安装r ...