Spring JSR-250注解
2015-07-27 23:49
603 查看
Spring也基于JSR-250注解,包括@PostConstruct,@PreDestroy和@Resource 注解。虽然这些注释都没有真正必需的,因为你已经有其他的候补,但还是让我给他们有关一个简单的想法。
要定义安装和拆卸一个bean,我们只是声明了初始化方法和/或销毁,<bean>方法的参数。在init-method属性指定一个方法,是被称为bean上后立即实例化。同样,销毁规定了被称为bean被从容器中取出之前的方法。
您可以使用@PostConstruct 注释的初始化回调的替代和@PreDestroyannotation 作为销毁回调的另一种如在下面的例子来说明。
让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
这里是HelloWorld.java 文件的内容:
以下是MainApp.java文件的内容。在这里,你需要注册一个关闭挂钩registerShutdownHook() 是在AbstractApplicationContext类中声明方法。这将确保正常关闭,并调用相关的destroy()方法。
下面是需要的init和destroy方法配置文件beans.xml文件:
创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,将打印以下信息:
您可以使用@Resource注解的字段或setter方法,它的工作原理相同在Java EE 5中。在@Resource注解有一个'name'属性将被解释为bean的名字被注入。可以说,它遵循名称自动装配这体现在下面的例子中的语义:
如果没有'name'被明确指定,默认名称是从字段名或者setter方法得出。如果是字段,它需要的字段名称,在出现一个setter方法,它需要的bean属性名。
转载自: http://www.yiibai.com/spring/spring_jsr250_annotations.html
@PostConstruct 和@PreDestroy 注解:
要定义安装和拆卸一个bean,我们只是声明了初始化方法和/或销毁,<bean>方法的参数。在init-method属性指定一个方法,是被称为bean上后立即实例化。同样,销毁规定了被称为bean被从容器中取出之前的方法。您可以使用@PostConstruct 注释的初始化回调的替代和@PreDestroyannotation 作为销毁回调的另一种如在下面的例子来说明。
例子
让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:步骤 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld and MainApp under the com.yiibai package. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
package com.yiibai; 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类中声明方法。这将确保正常关闭,并调用相关的destroy()方法。
package com.yiibai; 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(); } }
下面是需要的init和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-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.yiibai.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.
@Resource 注解:
您可以使用@Resource注解的字段或setter方法,它的工作原理相同在Java EE 5中。在@Resource注解有一个'name'属性将被解释为bean的名字被注入。可以说,它遵循名称自动装配这体现在下面的例子中的语义:package com.yiibai; 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属性名。
转载自: http://www.yiibai.com/spring/spring_jsr250_annotations.html
相关文章推荐
- 导入项目 R.java没有
- java学习笔记(五)collections--vector
- Debug---Eclipse断点调试基础
- myeclipse快捷键
- Java中文字编码问题详解
- Spring <context:annotation-config/> 解说
- java中i++与++i的区别
- Java的反射与RTTI(运行时类型信息)的区别
- 多态学习日志
- int&boolean——Java和C的一点小区别
- Eclipse报Caused by: java.lang.OutOfMemoryError: PermGen space解决思路
- [黑马程序员](第16天)eclipse及知识点补充
- Spring @Autowired注解
- Java内部类——成员内部类
- mac 下android开发 eclipse中找不到设备
- Spring IOC装配Bean的3种方式
- 用Eclipse和GDB构建ARM交叉编译和在线调试环境
- 使用eclipse搭建嵌入式开发环境
- Eclipse ARM IDE 开发环境
- 【深入Java虚拟机】之七:Javac编译与JIT编译