Spring中bean注入前后的一些操作:

InitializingBean 和 DisposableBean

init-method 和 destroy-method

@PostConstruct 和 @PreDestroy

In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.

  1. For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set.
  2. For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.

In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction.

Note
The @PostConstruct and @PreDestroy annotation are not belong to Spring, it’s located in the J2ee library – common-annotations.jar.

具体的使用

对于第一个:

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
 
public class CustomerService implements InitializingBean, DisposableBean
{
	String message;
 
	public String getMessage() {
	  return message;
	}
 
	public void setMessage(String message) {
	  this.message = message;
	}
 
	public void afterPropertiesSet() throws Exception {
	  System.out.println("Init method after properties are set : " + message);
	}
 
	public void destroy() throws Exception {
	  System.out.println("Spring Container is destroy! Customer clean up");
	}
 
}

  下面的例子展示了 init-method and destroy-method.

public class CustomerService
{
	String message;
 
	public String getMessage() {
	  return message;
	}
 
	public void setMessage(String message) {
	  this.message = message;
	}
 
	public void initIt() throws Exception {
	  System.out.println("Init method after properties are set : " + message);
	}
 
	public void cleanUp() throws Exception {
	  System.out.println("Spring Container is destroy! Customer clean up");
	}
 
}

  

<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 id="customerService" class="com.mkyong.customer.services.CustomerService" 
		init-method="initIt" destroy-method="cleanUp">
 
		<property name="message" value="i'm property message" />
	</bean>
 
</beans>

  第三种的使用:

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");
	}
 
}

  By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘<context:annotation-config />‘ in bean configuration file,

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.mkyong.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.mkyong.customer.services.CustomerService">
		<property name="message" value="i'm property message" />
	</bean>
 
</beans>

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBean对象的生命周期可以分为以下几个步骤: 1. 加载Bean定义:在Spring容器启动时,会读取配置文件或注解,将Bean定义加载到内存。这些定义描述了Bean的各种属性,如类的路径、初始化方法、销毁方法等。 2. 创建Bean实例:根据加载的Bean定义,Spring容器会使用合适的实例化策略创建Bean的实例。这可能涉及到使用构造函数、工厂方法或反射来实例化Bean。 3. 设置Bean属性:在创建Bean实例后,Spring容器会使用依赖注入的方式设置Bean的属性。这包括通过构造函数、setter方法或字段注入来为Bean的属性赋值。 4. 调用Bean的初始化方法:一旦Bean的所有属性都被设置好,Spring容器会调用Bean的初始化方法。这个初始化方法可以通过在Bean定义配置指定的方法名来调用,也可以实现InitializingBean接口来定义初始化逻辑。 5. 注册Bean后置处理器:在Bean的初始化方法调用之前Spring容器会注册Bean后置处理器,这些处理器可以在Bean初始化前后对Bean进行自定义处理。这个阶段的主要方法是registerBeanPostProcessors。 6. Bean的使用:一旦Bean的初始化方法调用完成,Bean就可以被应用程序使用了。 7. 销毁Bean:当Spring容器关闭时,会调用Bean的销毁方法进行资源的释放。这个销毁方法可以通过在Bean定义配置指定的方法名来调用,也可以实现DisposableBean接口来定义销毁逻辑。 总结来说,SpringBean对象的生命周期包括加载Bean定义、创建Bean实例、设置Bean属性、调用初始化方法、注册Bean后置处理器、Bean的使用和销毁Bean的过程。这些步骤确保了Bean在Spring容器的正确创建、初始化和销毁,从而实现了灵活的Bean管理和生命周期控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值