Spring基于Java的JSR-250注解

以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-jsr250-annotation.html

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>

<span style="color: rgba(255, 0, 0, 1)"><strong>&lt;!-- Spring Core --&gt;
&lt;!-- http://mvnrepository.com/artifact/org.springframework/spring-core --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
    &lt;version&gt;4.1.4.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
 
&lt;!-- Spring Context --&gt;
&lt;!-- http://mvnrepository.com/artifact/org.springframework/spring-context --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    &lt;version&gt;4.1.4.RELEASE&lt;/version&gt;
&lt;/dependency&gt;</strong></span>

</dependencies>
</project>

HelloWorld.java:

package com.jsoft.testspring.testannotationjsr250;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class HelloWorld {
private String messageString;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setMessage(String message){
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.messageString =<span style="color: rgba(0, 0, 0, 1)"> message;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getMessage(){
    System.out.println(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.messageString);
}

@PostConstruct
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initPost(){
    System.out.println(</span>"@ init"<span style="color: rgba(0, 0, 0, 1)">);
}

@PreDestroy
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> destroyPre(){
    System.out.println(</span>"@ destroy"<span style="color: rgba(0, 0, 0, 1)">);
}

}

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">
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="helloWorld"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="com.jsoft.testspring.testannotationjsr250.HelloWorld"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="Message"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="Hello World!"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">property</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:annotation-config</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

</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;

<span style="color: rgba(255, 0, 0, 1)"><strong>@Resource(name</strong></span></span><span style="color: rgba(255, 0, 0, 1)"><strong>="msg")
</strong></span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setMessage(String message){
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.messageString =<span style="color: rgba(0, 0, 0, 1)"> message;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getMessage(){
    System.out.println(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.messageString);
}

@PostConstruct
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initPost(){
    System.out.println(</span>"@ init"<span style="color: rgba(0, 0, 0, 1)">);
}

@PreDestroy
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> destroyPre(){
    System.out.println(</span>"@ destroy"<span style="color: rgba(0, 0, 0, 1)">);
}

}

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">
<span style="color: rgba(255, 0, 0, 1)"><strong>&lt;bean id="msg" class="java.lang.String"&gt;
    &lt;constructor-arg index="0" value="Hello World!"&gt;&lt;/constructor-arg&gt;
&lt;/bean&gt;</strong></span>
                
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="helloWorld"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="com.jsoft.testspring.testannotationjsr250.HelloWorld"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:annotation-config</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

</beans>

注意:当你使用 @Resource 注解时,此 name 指定的是一个 bean,也就是一个类才能生效,而不是这个 bean 上的具体 setter 方法。所以这里直接用 bean 生成了一个 String 的类,然后在类的实例化传入有参的构造函数中。

测试结果:

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test12/testannotationjsr250