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 ...
随机推荐
- 洛谷 P2604 [ZJOI2010]网络扩容
题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的最小扩容费用. ...
- 微信小程序开发系列七:微信小程序的页面跳转
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- 尺取法 || emmmm
给定两个上升的数组,一个数组任取一个数,求两个数差的min 尺取法emm 也不知道对不对 #include <stdio.h> #include <stdlib.h> #def ...
- 题目:企业发放的奖金根据利润提成。 利润(I)低于或等于10万元时,奖金可提10%; 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%; 20万到40万之间时,高于20万元的部分,可提成5%; 40万到60万之间时高于40万元的部分,可提成 3%; 60万到100万之间时,高于60万元的部分,可提成1.5%; 高于100万元时,超过
题目:企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%: 20万到 ...
- postman使用--添加headers、授权、cookies
添加headers Request Headers(请求头)用来说明服务器要使用的附加信息,比较重要的信息有:Cookie,Referer,User-Agent等.在postman中可以在请求下方的H ...
- 2019西安多校联训 Day5
T1 光哥为了不让某初二奆佬恶心到我们而留下的火种 (貌似没这题平均分就100-了) 思路:就一横一竖让后就gztopa嘛 #include <bits/stdc++.h> using n ...
- SqlSugar直接执行Sql
参考:http://www.codeisbug.com/Doc/8/1132 我的思路: 1.数据库中写好sql 2.用SqlSugar直接执行sql,获取DataTable的数据 3.DataTab ...
- mysql5.7配置
my3306.cnf [client] port = 3306 #端口socket = /data/mysql3306/mysql3306.sock #mysql以socket方式运行的soc ...
- 页面布局排版-block,inline,float,relative,absolute等
1.span和div的区别 div是块元素(block),span是行内元素(inline): span什么事也不会做,它存在的目的在与为开发者给它所围绕的元素指定样式.div类似,不过它引入了行分隔 ...