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 ...
 
随机推荐
- Traceroute侦测主机到目的主机之间所经路由情况的重要工具
			
ICMP的应用--Traceroute Traceroute是用来侦测主机到目的主机之间所经路由情况的重要工具,也是最便利的工具.前面说到,尽管ping工具也可以进行侦测,但是,因为ip头的限制,pi ...
 - Windows程序设计2(消息机制、菜单)
			
一 .小记; PostQuitMessage(0); 产生WM_QUIT消息给进程队列,且立即返回,同时使得消息循环退出,使得进程终止.(其实它通过PostMessage(hWnd,WM_QUIT,0 ...
 - XtraBackUp 热备份工具
			
是一款强大的在线热备份工具 备份的过程中,不锁表 使用percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm yum源安装: 1.安装Percona的库: ...
 - BI结构图及自动建表结构图
 - ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath
			
问题: ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the ...
 - QT5:第二章 布局排版控件
			
一.简介 在QT组件面板中有Layouts和Spacers两个组件面板 注意:布局排版控件不显示 1.Layouts(布局) Vertical Layout:垂直方向布局,组件自动在垂直方向上分布 H ...
 - git命令使用(三)
			
git的使用--分支的使用 我们都知道拉取代码的时候,拉下来的是默认的分支,但我们需要的是,其他分支的使用操作 开始,拉取项目 git clone url 查看分支,显示默认分支 git branch ...
 - 条款5:了解C++默默编写并调用哪些函数(Know what functions C++ silently writes and calls)
			
1.default costructor / copy constructor / copy assignment 者三者的区别? 特别是copy constructor & copy as ...
 - 第三章:systemverilog文本值和数据类型
			
1.增强的文本值 2.改进的`define文本替换 3.时间值 4.新的变量类型 5.有符号和无符号类型 6.静态和动态变量(***) 7.类型转换 8.常数 增强的文本值(文本赋值增强) 主要是:位 ...
 - Elastic-Job-Lite 源码分析 —— 作业分片策略
			
摘要: 原创出处 http://www.iocoder.cn/Elastic-Job/job-sharding-strategy/ 「芋道源码」欢迎转载,保留摘要,谢谢! 本文基于 Elastic-J ...