您的位置:首页 > 编程语言 > Java开发

好记性不如烂笔头91-spring3学习(12)-bean的PostConstruct和PreDestroy

2015-03-20 17:22 471 查看
Spring从2.5开始支持@PostConstruct和@PreDestroy注解。他们的功能相当于init-method和destroy-method,但是在一个Bean中,可以定义多个@PostConstruct和@PreDestroy。

就我个人的观点来说,我是不希望我的团队在代码中大量使用这些方法。这些方法,总是让人感觉代码是在以跳跃的方式进行运作,而不是基于常规的逻辑。

漏洞和一些预料之外的方法,总是出现在这样的配置之中….

使用多个PostConstruct和PreDestroy的源代码

[code]驾驶员
package com.spring.bean;

import org.springframework.stereotype.Component;

/**
 * 一个简单通过注解配置的bean,驾驶员
 * @author 范芳铭
 * 
 * */
@Component
public class People {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

小汽车
package com.spring.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 一个简单通过注解配置的bean
 * @author 范芳铭
 * 
 * */
@Component
public class Car {
    private People people;

    public Car(){
        System.out.println("Car:默认构造函数");
    }

    public People getPeople() {
        return people;
    }

    @Autowired
    public void setPeople(People people) {
        System.out.println("-- @Autowired 参数注入");
        this.people = people;
    }

    @PostConstruct
    private void ini1(){
        System.out.println("-- 运行 ini1.");
    }

    @PostConstruct
    private void ini2(){
        System.out.println("-- 运行 ini2.");
    }

    @PreDestroy
    private void destory1(){
        System.out.println("-- 运行 destory1.");
    }

    @PreDestroy
    private void destory2(){
        System.out.println("-- 运行 destory2.");
    }

    @Override
    public String toString(){
        return "Role类被调用: "+this.people.getName();
    }

}


模拟容器启动和关闭

[code]package com.spring.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EasySpringContext {
    public static void main(String[] args){
        //模拟启动容器
        ApplicationContext ctx = new
                ClassPathXmlApplicationContext("bean.xml");
        //模拟容器关闭
        ((ClassPathXmlApplicationContext)ctx).destroy();
    }
}


配置bean.xml

[code]<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    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         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/task         http://www.springframework.org/schema/task/spring-task.xsd"> 
   <context:component-scan base-package="com.spring.bean"/>    

</beans>


运行结果

Car:默认构造函数

– @Autowired 参数注入

– 运行 ini1.

– 运行 ini2.

– 运行 destory1.

– 运行 destory2.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: