【spring源码学习】Spring @PostConstruct和@PreDestroy实例
注:@PostConstruct和@PreDestroy 标注不属于 Spring,它是在J2EE库- common-annotations.jar。
@PostConstruct 和 @PreDestroy
package com.yiibai.customer.services; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; public class CustomerService
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @PostConstruct
public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
} @PreDestroy
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
} }
1. CommonAnnotationBeanPostProcessor
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="customerService" class="com.yiibai.customer.services.CustomerService">
<property name="message" value="i'm property message" />
</bean> </beans>
2. <context:annotation-config />
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="customerService" class="com.yiibai.customer.services.CustomerService">
<property name="message" value="i'm property message" />
</bean> </beans>
执行结果
package com.yiibai.common; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.customer.services.CustomerService; public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close();
}
}
输出结果
Init method after properties are set : im property message
com.yiibai.customer.services.CustomerService@47393f
...
INFO: Destroying singletons in org.springframework.beans.factory.
support.DefaultListableBeanFactory@77158a:
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up
【spring源码学习】Spring @PostConstruct和@PreDestroy实例的更多相关文章
- spring源码学习——spring整体架构和设计理念
Spring是在Rod Johnson的<Expert One-On-One J2EE Development and Design >的基础上衍生而来的.主要目的是通过使用基本的java ...
- Spring源码学习
Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...
- Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点
Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...
- spring源码学习之路---深入AOP(终)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...
- spring源码学习之路---IOC初探(二)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...
- Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md
写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...
- Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...
- Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作
写在前面 上文 Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件主要讲Spring容器创建时通过XmlBeanDefinitionReader读 ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- 【目录】Spring 源码学习
[目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...
随机推荐
- Flask 使用富文本输入框
模板 <script src="{{ url_for('static', filename='ckeditor/ckeditor.js') }}"></scrip ...
- Linux 查看系统所有用户
grep bash /etc/passwd Linux 查看系统所有用户
- Python面试题之Python反射详解
0x00 前言 反射,可以理解为利用字符串的形式去对象中操作成员属性和方法 反射的这点特性让我联想到了exec函数,也是把利用字符串的形式去让Python解释器去执行命令 Python Version ...
- 初识PHP(一)基础语法
一直准备学习PHP,结果前一段时间总是有事情,耽误了一阵子.现在赶快迎头赶上! 这个系列只是谈谈我对于PHP的一些看法,不是教程性质的.另外我是小白,只是写写随笔,大神求轻拍.本人学习过c .java ...
- ubuntu14.04安装CUDA8.0
ubuntu安装CUDA 因为深度学习需要用到CUDA,所以写篇博客,记录下自己安装CUDA 的过程. 1 安装前的检查 安装CUDA之前,首先要做一些事情,检查你的机器是否可以安装CUDA. 1.1 ...
- VC++使用FindFirstFile,FindNextFile遍历一个文件夹
转载:http://www.cnblogs.com/chenkunyun/archive/2012/03/24/2415727.html 方法一. //遍历文件夹函数 void TraverseFol ...
- UVA 12338 Anti-Rhyme Pairs(hash + 二分)题解
题意:给出两个字符串的最大相同前缀. 思路:hash是要hash,不hash是不可能的.hash完之后从头遍历判断超时然后陷入沉默,然后告诉我这能二分orz,二分完就过了,写二分条件写了半天.不要用数 ...
- iOS动画进阶 - 手摸手教你写ShineButton动画
移动端访问不佳,请访问我的个人博客 前段时间在github上看见一个非常nice的动画效果,可惜是安卓的,想着用swift写一个iOS版的,下下来源代码研究了一下,下面是我写代码的心路历程 先上图和d ...
- linux开启nscd服务缓存加速
在我使用的阿里云主机上有观察到开启了一个服务nscd ,后来谷哥了下该服务的作用.了解到nscd会缓存三种服务passwd group hosts,所以它会记录三个库,分别对应源/etc/passwd ...
- python应用-掷骰子模拟-pygal
pygal安装: Linux下: pip install pygal Windows下: python -m pip install pygal 效果如图: # -*- coding: utf-8 - ...