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

时间: 2024-08-10 06:28:35

spring之Hooking to bean life cycles的相关文章

看看Spring的源码——Bean加载过程

最近几天跟同事聊起Spring的一些问题,对一些地方有些疑问,趁这两天有点空,看看Spring的源码,了解下具体的实现细节.本文基于Spring 4.0.5版本. 首先Web项目使用Spring是通过在web.xml里面配置org.springframework.web.context.ContextLoaderListener初始化IOC容器的. <listener> <listener-class>org.springframework.web.context.ContextL

【Spring】的【Bean】管理(注解)【四个相同功能的注解】

[Spring]的[Bean]管理(注解)[四个相同功能的注解] 注解:代码里面特殊的标记,使用注解也可以完成一些相关的功能. 注解写法:@注解名称(属性名称=属性值) 注解使用在类.方法.属性上面 (注解可以替代配置文件,并非完全替代): 1.创建类,创建方法 1 public class User { 2 public void add(){ 3 System.out.println("add-----------"); 4 } 5 } 2.创建spring配置文件,引入约束 1

半夜思考之查漏补缺, 在 Spring中, 所有的bean都是Spring创建的吗?

Spring 是一个 bean 容器, 负责 bean的创建, 那么所有的 bean对象都是 Spring 容器创建的吗? 答案是否定的. 但是乍一想, 好像所有的对象都是 Spring 容器负责创建并注入的, 今天在看书的时候, 看到了创建 bean 的三种方式, 分别是 : 使用构造器创建 bean实例 使用静态工厂方法创建 bean 调用实例工厂方法创建 bean 第一个是 Spring 使用反射创建 bean 对象, class属性指定的是 Bean实现类 ; 第二个的class属性指定

spring实战之装配bean

内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spring,那么它就是一个空的容器,所以需要配置spring来告诉容器它需要加载哪些Bean和如何装配这些bean,这样才能确保它们能够彼此协作. 从spring3.0开始,spring容器提供了两种配置bean的方式.第一种是传统上的使用一个或多个XML文件作为配置文件.第二种提供了基于java注解的配

Spring基础知识及bean的配置

IOC与DI: IOC(inversion of control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC之后,则是容器主动地将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接收资源.这种方式也被称为查找的被动形式.DI(dependency injection):IOC的另一种表述形式,即组件以一些预先定义好的方式(例如setter方法)接受来自如容器的资源注入.相对于IOC而言,这种表述更直接

Spring之自动装配bean

Spring之自动装配bean 最近学习Spring框架,参考资料是Spring IN ACTION----第一张内容飘过去~~ 从第二章的自动装配bean开始,不过学习Spring核心最重要的还是ioc的注入模式吧! 书上是这么说的----(概念问题,哈哈),首先普及几个概念 --------------------------------------------------------------------------------------------------------------

spring什么时候实例化bean

简要说明spring什么时候实例化bean,首先要分2种情况  第一:如果你使用BeanFactory作为Spring Bean的工厂类,则所有的bean都是在第一次使用该Bean的时候实例化  第二:如果你使用ApplicationContext作为Spring Bean的工厂类,则又分为以下几种情况:       (1):如果bean的scope是singleton的,并且lazy-init为false(默认是false,所以可以不用设置),则ApplicationContext启动的时候就

从头认识Spring-1.3 Spring的容器与Bean

这一章节我们来讨论一下Spring的容器与Bean. 1.Spring的容器 (1)BeanFactory 这个是由org.springframework.beans.factory.BeanFactory来实现的 (2)ApplicationContext 这个是由org.springframework.context.ApplicationContext来实现的 我们在一般的编程当中,都是以ApplicationContext为主,因为它提供的方法更加的方便. 2.ApplicationCo

Spring系列之装配Bean

一.概述 容器是Spring框架的核心,Spring容器使用IOC管理所有组成应用系统的组件.Spring有两种不同的容器:BeanFactory提供最简单的容器,提供了最基础的依赖注入支持,ApplicationContext建立在BeanFactory的基础之上,提供了系统构架服务如从属性文件中读取文本信息,事件传递等. 在Spring容器中拼凑Bean叫做装配,装配Bean的时候,你是在告诉容器需要哪些Bean以及容器如何使用依赖注入将它们配合在一起. 二.装配Bean 2.1  使用XM