spring还使用基于 jsr-250 注释,它包括 @postconstruct, @predestroy 和 @resource 注释。因为你已经有了其他的选择,尽管这些注释并不是真正所需要的,但是关于它们仍然让我给出一个简短的介绍。
为了定义一个 bean 的安装和卸载,我们使用 init-method 和/或 destroy-method 参数简单的声明一下
你可以使用 @postconstruct 注释作为初始化回调函数的一个替代,@predestroy 注释作为销毁回调函数的一个替代,其解释如下示例所示。
让我们使 eclipse ide 处于工作状态,请按照下列步骤创建一个 spring 应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为 springexample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。 |
2 | 使用 add external jars 选项添加所需的 spring 库文件,就如在 spring hello world example 章节中解释的那样。 |
3 | 在 com.tutorialspoint 包下创建 java 类 helloworld 和 mainapp。 |
4 | 在 src 文件夹下创建 beans 配置文件 beans.xml。 |
5 | 最后一步是创建所有 java 文件和 bean 配置文件的内容,并且按如下解释的那样运行应用程序。 |
这里是 helloworld.java 文件的内容:
package com.tutorialspoint;
import javax.annotation.*;
public class helloworld {
private string message;
public void setmessage(string message){
this.message = message;
}
public string getmessage(){
system.out.println("your 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.");
}
}
下面是 mainapp.java 文件的内容。这里你需要注册一个关闭钩 registershutdownhook() 方法,该方法在 abstractapplicationcontext 类中被声明。这将确保一个完美的关闭并调用相关的销毁方法。
package com.tutorialspoint;
import org.springframework.context.support.abstractapplicationcontext;
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/>
<bean id="helloworld"
class="com.tutorialspoint.helloworld"
init-method="init" destroy-method="destroy">
<property name="message" value="hello world!"/>
</bean>
</beans>
一旦你在源文件和 bean 配置文件中完成了上面两处改变,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:
bean is going through init.
your message : hello world!
bean will destroy now.
你可以在字段中或者 setter 方法中使用 @resource 注释,它和在 java ee 5 中的运作是一样的。@resource 注释使用一个 ‘name’ 属性,该属性以一个 bean 名称的形式被注入。你可以说,它遵循 by-name 自动连接语义,如下面的示例所示:
package com.tutorialspoint;
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 属性名称。