Spring Annotation(五)
使用 JSR-250 的注释
Spring 不但支持自己定义的 @Autowired
的注释,还支持几个由 JSR-250 规范定义的注释,它们分别是 @Resource
、@PostConstruct
以及 @PreDestroy
。
@Resource
@Resource
的作用相当于 @Autowired
,只不过 @Autowired
按 byType 自动注入,面 @Resource
默认按 byName 自动注入罢了。@Resource
有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource
注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。
Resource 注释类位于 Spring 发布包的 lib/j2ee/common-annotations.jar 类包中,因此在使用之前必须将其加入到项目的类库中。来看一个使用 @Resource
的例子:
清单 16. 使用 @Resource 注释的 Boss.java
package com.baobaotao;
import javax.annotation.Resource;
public class Boss {
// 自动注入类型为 Car 的 Bean
@Resource
private Car car;
// 自动注入 bean 名称为 office 的 Bean
@Resource(name = "office")
private Office office;
}
|
一般情况下,我们无需使用类似于 @Resource(type=Car.class)
的注释方式,因为 Bean 的类型信息可以通过 Java 反射从代码中获取。
要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
:
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
|
CommonAnnotationBeanPostProcessor
实现了 BeanPostProcessor
接口,它负责扫描使用了 JSR-250 注释的 Bean,并对它们进行相应的操作。
@PostConstruct 和 @PreDestroy
Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,您既可以通过实现 InitializingBean/DisposableBean 接口来定制初始化之后 / 销毁之前的操作方法,也可以通过 <bean> 元素的 init-method/destroy-method 属性指定初始化之后 / 销毁之前调用的操作方法。关于 Spring 的生命周期,笔者在《精通 Spring 2.x—企业应用开发精解》第 3 章进行了详细的描述,有兴趣的读者可以查阅。
JSR-250 为初始化之后/销毁之前方法的指定定义了两个注释类,分别是 @PostConstruct 和 @PreDestroy,这两个注释只能应用于方法上。标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
清单 17. 使用 @PostConstruct 和 @PreDestroy 注释的 Boss.java
package com.baobaotao;
import javax.annotation.Resource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Boss {
@Resource
private Car car;
@Resource(name = "office")
private Office office;
@PostConstruct
public void postConstruct1(){
System.out.println("postConstruct1");
}
@PreDestroy
public void preDestroy1(){
System.out.println("preDestroy1");
}
…
}
|
您只需要在方法前标注 @PostConstruct
或 @PreDestroy
,这些方法就会在 Bean 初始化后或销毁之前被 Spring 容器执行了。
我们知道,不管是通过实现 InitializingBean
/DisposableBean
接口,还是通过 <bean> 元素的 init-method/destroy-method
属性进行配置,都只能为 Bean 指定一个初始化 / 销毁的方法。但是使用 @PostConstruct
和 @PreDestroy
注释却可以指定多个初始化 / 销毁方法,那些被标注 @PostConstruct
或 @PreDestroy
注释的方法都会在初始化 / 销毁时被执行。
通过以下的测试代码,您将可以看到 Bean 的初始化 / 销毁方法是如何被执行的:
清单 18. 测试类代码
package com.baobaotao;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnoIoCTest {
public static void main(String[] args) {
String[] locations = {"beans.xml"};
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
ctx.destroy();// 关闭 Spring 容器,以触发 Bean 销毁方法的执行
}
}
|
这时,您将看到标注了 @PostConstruct
的 postConstruct1()
方法将在 Spring 容器启动时,创建 Boss
Bean 的时候被触发执行,而标注了 @PreDestroy
注释的 preDestroy1()
方法将在 Spring 容器关闭前销毁 Boss
Bean 的时候被触发执行。
分享到:
相关推荐
Spring annotation 样例
Spring Annotation
spring annotation注解 使用Spring注解来注入属性
spring的Annotation注解.
NULL 博文链接:https://cyhcheng.iteye.com/blog/390551
Spring的Annotation配置相关讲义
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
Spring_Annotation_AOP
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
Spring IOC Annotation 注入 学习实例
spring annotation 从入门到精通
spring annotation 注解 maven 配套的源码
spring annotation example source code java maven
Spring注解开发代码+脑图+源码分析+spingmvc源码分析
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记