标签:predestroy bean 注解 spring 好记性不如烂笔头
Spring从2.5开始支持@PostConstruct和@PreDestroy注解。他们的功能相当于init-method和destroy-method,但是在一个Bean中,可以定义多个@PostConstruct和@PreDestroy。
就我个人的观点来说,我是不希望我的团队在代码中大量使用这些方法。这些方法,总是让人感觉代码是在以跳跃的方式进行运作,而不是基于常规的逻辑。
漏洞和一些预料之外的方法,总是出现在这样的配置之中….
驾驶员
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();
}
}
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
<?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.
好记性不如烂笔头91-spring3学习(12)-bean的PostConstruct和PreDestroy
标签:predestroy bean 注解 spring 好记性不如烂笔头
原文地址:http://blog.csdn.net/ffm83/article/details/44494821