Spring JSR-250 注释

Spring还使用基于 JSR-250 注释,它包括

  • @PostConstruct 注释

  • @PreDestroy 注释

  • @Resource 注释

@PostConstruct 和 @PreDestroy 注释

为了定义一个 bean 的安装和卸载,我们使用 init-method 和/或 destroy-method 参数简单的声明一下 。

init-method 属性指定了一个方法,该方法在 bean 的实例化阶段会立即被调用。

同样地,destroy-method 指定了一个方法,该方法只在一个 bean 从容器中删除之前被调用。

你可以使用 @PostConstruct 注释作为初始化回调函数的一个替代,@PreDestroy 注释作为销毁回调函数的一个替代。

其解释如下示例所示

  • 新建Spring项目

  • 创建 Java 类 HelloWorld 和 MainApp

这里是 HelloWorld.java 文件的内容:

package hello;
import javax.annotation.*;

public class HelloWorld {
    private String message;
    public void setMessage(String message){
        this.message = message;
    }
    public String getMessage(){
        System.out.println("the  Message : " + message);
        return message;
    }
    @PostConstruct
    public void init(){
        System.out.println("Bean is going through init");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Bean will destroy now.");
    }
}

注:需要在文件开头使用:

import javax.annotation.*;

引入相关注释。

下面是 MainApp.java 文件的内容。

这里你需要注册一个关闭钩 registerShutdownHook() 方法,该方法在 AbstractApplicationContext 类中被声明。

这将确保一个完美的关闭并调用相关的销毁方法。

package hello;
import org.springframework.context.support.AbstractApplicationContext;
//import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        AbstractApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.registerShutdownHook();
    }
}

下面是配置文件 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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<!-- Definition for  bean -->
<bean id="helloWorld" class="hello.HelloWorld"
        init-method="init" destroy-method="destroy">
    <property name="message" value="你好!"/>
</bean>

</beans>

运行结果

Bean is going through init
the  Message : 你好!
Bean will destroy now.

Process finished with exit code 0

@Resource 注释

可以在字段中或者 setter 方法中使用 @Resource 注释。

@Resource 注释使用一个 ‘name’ 属性,该属性以一个 bean 名称的形式被注入。

也可以说,它遵循 by-name 自动连接语义,如下面的示例所示

package hello;
import javax.annotation.Resource;

public class TextEditor {
   private SpellChecker spellChecker;
   @Resource(name= "spellChecker")
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker(){
      return spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

如果没有明确地指定一个 ‘name’,默认名称源于字段名或者 setter 方法。

  • 在字段的情况下,它使用的是字段名;

  • 在一个 setter 方法情况下,它使用的是 bean 属性名称。

每天学习一点点,每天进步一点点。

原文地址:https://www.cnblogs.com/youcoding/p/12790286.html