Spring 基于注解的配置(三)(@Resource,@PostConstruct ,@PreDestory)

来源:互联网 发布:矩阵霸主音响套装 编辑:程序博客网 时间:2024/05/17 21:42

@Resource

简介:Spring也用提供JSR-250 @Resource 注解在属性或者Bean setter方法上使用,它是JavaEE 5和 JavaEE 6的公共部分
@Resource注解:采用按名字装配的语义(采用Spring默认配置的Bean的名字,常见的为首字母大写)
应用范围:在字段或者setter方法中使用
示例一:
Computer.java

package com.spring.Autowire;/** * Created by ruanjianlin on 2017/10/6. */public class Computer {    public Computer() {       System.out.println("You get a new computer");}public void play(){    System.out.println("The computer is working! ");}}

Human.java

package com.spring.Autowire;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import javax.annotation.Resource;/** * Created by ruanjianlin on 2017/10/6. */public class Human {    @Resource private Computer computer;@Autowired@Qualifier("manSing")private Sing sing;public Human() {    System.out.println("The function of  Human constructor is working!");}public Human(Computer computer) {    this.computer = computer;    System.out.println("The function of  Human which has a parameter is working!");}public Computer getComputer() {    return computer;}public void setComputer(Computer computer) {    this.computer = computer;    System.out.println("The function of setComputer is working!");}public void setSing(Sing sing) {    this.sing = sing;}public void play(){    computer.play();    sing.sing();}}

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"><context:annotation-config/><context:component-scan base-package="com.spring" use-default-filters="false">    <context:include-filter type="regex" expression="com.spring.Autowire.*"/></context:component-scan>

Spring默认的Bean的name为原名字首字母变小写,及Computer自动注册后名字为computer

If no name is specified explicitly, the default name is derived from the field name or setter method. In
case of a field, it takes the field name; in case of a setter method, it takes the bean property name.

解析:如果没有名字被明确规定,默认的名字将会从属性的名字或者setter方法中衍生,如果在属性上使用该注解那么它将会依据该名字去寻找Bean装配,,同理如果其在setter方法上使用,则其将会使用setter方法中的参数名称去寻找Bean完成装配

若通过名字无法匹配怎么办?

In the exclusive case of @Resource usage with no explicit name specified, and similar to @Autowired,
@Resource finds a primary type match instead of a specific named bean and resolves well known resolvable dependencies: the BeanFactory, ApplicationContext, ResourceLoader,
ApplicationEventPublisher, and MessageSource interfaces.

解析:如果没有具体的Bean名字可以匹配,那么此时@Resource将会与@Autowired注解类似,该注解按类型进行匹配而不是按名字匹配Bean,借助于此解决众所周知的可以解决的依赖项BeanFactory, ApplicationContext, ResourceLoader,ApplicationEventPublisher, 和 MessageSource 接口。

@PostConstruct @PreDestory注解

CommonAnnotationBeanPostProcessor 不仅可以识别@Resource注解,其也可以识别JSR-250(Java Specification Requests——java 规范要求)有关生命周期的注解
xml中的配置:
Init-method 指定Bean初始化时调用的方法
destory-method 指定Bean销毁时调用的方法
Java中
@PostConstruct 初始化回调函数的一个替代(构造函数之后调用)
@PreDestory 销毁回调函数的一个替代(最后调用)
示例:(在上述源代码基础上修改)
Human.java

package com.spring.Autowire;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;/** * Created by ruanjianlin on 2017/10/6. */public class Human {@Resourceprivate Computer com;@Autowired@Qualifier("manSing")private Sing sing;@PostConstructpublic void init(){    System.out.println("init");}@PreDestroypublic void destory(){    System.out.println("destory");}public Human() {    System.out.println("The function of  Human constructor is working!");}public Human(Computer computer) {    this.com = computer;    System.out.println("The function of  Human which has a parameter is working!");}public Computer getComputer() {    return com;}public void setComputer(Computer computer) {    this.com = computer;    System.out.println("The function of setComputer is working!");}public void setSing(Sing sing) {    this.sing = sing;}public void play(){    com.play();    sing.sing();}}

主要测试的方法:

Human human=(Human) app.getBean("human");human.play();human.destory();

运行结果:

You get a new computerThe function of  Human constructor is working!initThe computer is working! This is a man singing!destory

由结果可以看出先调用构造函数后调用初始化的回调方法,最后调用销毁的回调方法

Bean实例化与销毁时采用@PostConstruct ,@PreDestory注解与Init-method,destory-method的比较:
InitTest.java

package com.spring.demo;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;/** * Created by ruanjianlin on 2017/10/8. */public class InitTest {    public InitTest() {    System.out.println("InitTest()");}public void initialize(){    System.out.println("init-method");}@PostConstructpublic void init(){    System.out.println("@PostConstruct");}@PreDestroypublic void destory(){    System.out.println(" @PreDestroy");}public void destoryMethod(){    System.out.println("destory-method");}}

xml配置文件:

    <bean id="initTest" class="com.spring.demo.InitTest" init-method="initialize" destroy-method=""> </bean>

运行结果:

InitTest()@PostConstructinit-method @PreDestroy

结果分析:
运行顺序为:

  1. 构造函数
  2. @PostConstruct初始化回调函数
  3. init-method注册初始化方法
  4. @PreDestroy销毁回调函数
阅读全文
0 0
原创粉丝点击