spring之Hooking to bean life cycles

spring之Hooking to bean life cycles


Often, in enterprise application development, developers will want to plug in some extra functionality to be executed just after the construction and before the destruction of a business service. Spring provides multiple methods for interacting with such stages in the life cycle of a bean.

Implementing InitializingBean and DisposableBean

The Spring IoC container invokes the callback methods afterPropertiesSet() of
org.springframework.beans.factory.InitializingBean and destroy() of org.springframework.beans.factory.DisposableBean on any Spring bean and implements them:


public class UserServiceImpl implements UserService, InitializingBean,
DisposableBean {
	...
	@Override
	public void afterPropertiesSet() throws Exception
	{
		logger.debug( this + ".afterPropertiesSet() invoked!" );
/* Your initialization code goes here.. */
	}


	@
	Override
	public void destroy() throws Exception
	{
		logger.debug( this + ".destroy() invoked!" );
/* Your cleanup code goes here.. */
	}


	...
}


Annotating @PostConstruct and @PreDestroy on @Components

Spring supports JSR 250 @PostConstruct and @PreDestroy annotations on any Spring bean in an annotation supported environment, as shown here. Spring encourages this approach over implementing Spring-specific interfaces, as given in the previous section:

@Service
public class AnnotatedTaskService implements TaskService {
	...
	@PostConstruct
	public void init()
	{
		logger.debug( this.getClass().getName() + " started!" );
	}


	@
	PreDestroy
	public void cleanup()
	{
		logger.debug( this.getClass().getName() + " is about to destroy!" );
	}


	...
}


The init-method and destroy-method attributes of <bean/>


If you are using XML-only bean configuration metadata, then your best option is to declare init-method and destroy-method attributes on your <bean/> tags:

<bean id = "xmlTaskService" class = "com…XmlDefinedTaskService" 
               initmethod = "init" 
               destroy - method = "cleanup" >
												       ...
< / bean>

Container-level default-init-method and default-destroy-method


You can even set container-level default init and destroy methods so that you don’t need to set it for each bean. The container invokes these methods on beans only if they are present:
<beans default - init - method = "init" default - destroy - method = "cleanup" >
								     ...
< / beans>



读书笔记:

Spring Essentials

Copyright © 2016 Packt Publishing

First published: February 2016
Production reference: 1230216
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-234-9
www.packtpub.com


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值