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

posted @ 2017-05-24 16:44  EasonJim  阅读(5152)  评论(0编辑  收藏  举报