好记性不如烂笔头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的源代码
模拟容器启动和关闭
配置bean.xml
运行结果
Car:默认构造函数
– @Autowired 参数注入
– 运行 ini1.
– 运行 ini2.
– 运行 destory1.
– 运行 destory2.
就我个人的观点来说,我是不希望我的团队在代码中大量使用这些方法。这些方法,总是让人感觉代码是在以跳跃的方式进行运作,而不是基于常规的逻辑。
漏洞和一些预料之外的方法,总是出现在这样的配置之中….
使用多个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.
相关文章推荐
- 好记性不如烂笔头84-spring3学习(5)-spring的ApplicationContext(应用上下文)
- 好记性不如烂笔头95-spring3学习(16)-spring配置log4j
- 好记性不如烂笔头92-spring3学习(13)-spring和hibernate访问数据库的小差异
- 好记性不如烂笔头94-spring3学习(15)-用spring的jdbcTemplate简单访问数据库
- 好记性不如烂笔头12-使用HttpServletResponse生成验证码
- 好记性不如烂笔头83-spring3学习(4)-spring的BeanFactory(IoC容器)
- 好记性不如烂笔头87-spring3学习(8)-IoC的理解和比喻
- 好记性不如烂笔头85-spring3学习(6)-BeanFactory 于bean生命周期
- 好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期
- 好记性不如烂笔头17-java中的javaBean
- 好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期
- 好记性不如烂笔头之 ——vi命令
- Oracle函数小全||记录下来,用处多多啊——好记性不如烂笔头
- 好记性不如烂笔头
- 好记心不如烂笔头之jQuery学习,第一章
- 好记性不如烂笔头——datagridview相关
- 好记性不如烂笔头之 ——CP命令
- 谈好记性不如烂笔头
- 好记心不如烂笔头之JQuery学习,第三章
- 好记性不如烂笔头