Spring中Bean的初始化过程

本文详细介绍了Spring框架中Bean的生命周期,包括Bean的实例化、配置、初始化和销毁等关键步骤。并通过一个具体示例展示了各个回调方法的调用顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在传统的Java应用中,Bean的生命周期非常简单。Java的关键词new用来实例化Bean(或许他是非序列化的)。这样就够用了。相反,Bean 的生命周期在Spring容器中更加细致。理解Spring Bean的生命周期非常重要,因为你或许要利用Spring提供的机会来订制Bean的创建过程。 
 
1. 容器寻找Bean的定义信息并且将其实例化。 
2.受用依赖注入,Spring按照Bean定义信息配置Bean的所有属性。 
3.如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。 
4.如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。 
5.如果BeanPostProcessor和Bean关联,那么它们的postProcessBeforeInitialzation()方法将被调用。 
6.如果Bean指定了init-method方法,它将被调用。 
7.最后,如果有BeanPsotProcessor和Bean关联,那么它们的postProcessAfterInitialization()方法将被调用。 
    到这个时候,Bean已经可以被应用系统使用了,并且将被保留在Bean Factory中知道它不再需要。有两种方法可以把它从Bean Factory中删除掉。 
1.如果Bean实现了DisposableBean接口,destory()方法被调用。 
2.如果指定了订制的销毁方法,就调用这个方法。 
    Bean在Spring应用上下文的生命周期与在Bean工厂中的生命周期只有一点不同,唯一不同的是,如果Bean实现了ApplicationContextAwre接口,setApplicationContext()方法被调用。 
 
举例:(这里只是演示Bean工厂的例子) 
我这里用的是spring-1.2.6,由于版本原因吧,这里演示的不是很好! 
1.HelloWorld.java 

Java代码  收藏代码

  1. package com.spring.lifecycle;  

  2.   

  3. import org.springframework.beans.BeansException;  

  4. import org.springframework.beans.factory.BeanFactory;  

  5. import org.springframework.beans.factory.BeanFactoryAware;  

  6. import org.springframework.beans.factory.BeanNameAware;  

  7. import org.springframework.beans.factory.DisposableBean;  

  8. import org.springframework.beans.factory.InitializingBean;  

  9. import org.springframework.beans.factory.config.BeanPostProcessor;  

  10.   

  11. public class HelloWorld implements BeanNameAware,BeanFactoryAware, BeanPostProcessor,InitializingBean,DisposableBean{  

  12.   

  13.     private String hello;  

  14.   

  15.     public void setBeanName(String arg0) {  

  16.         System.out.println("调用BeanNameAware的setBeanName()..." );  

  17.     }  

  18.   

  19.     public String getHello() {  

  20.         return hello;  

  21.     }  

  22.   

  23.     public void setHello(String hello) {  

  24.         this.hello = hello;  

  25.         System.out.println("调用setHello()..." );  

  26.     }  

  27.   

  28.     public void customInit() {  

  29.         System.out.println("调用customInit()...");  

  30.     }  

  31.   

  32.     public void customDestroy() {  

  33.         System.out.println("调用customDestroy()...");  

  34.     }  

  35.   

  36.     public Object postProcessAfterInitialization(Object arg0, String arg1)  

  37.             throws BeansException {  

  38.         System.out.println("调用BeanPostProcessor的postProcessAfterInitialization()...");  

  39.         return null;  

  40.     }  

  41.   

  42.     public Object postProcessBeforeInitialization(Object arg0, String arg1)  

  43.             throws BeansException {  

  44.         System.out.println("调用BeanPostProcessor的postProcessBeforeInitialization()...");  

  45.         return null;  

  46.     }  

  47.   

  48.     public void destroy() throws Exception {  

  49.         System.out.println("调用DisposableBean的destory()...");  

  50.     }  

  51.   

  52.     public void afterPropertiesSet() throws Exception {  

  53.         System.out.println("调用InitializingBean的afterPropertiesSet()...");  

  54.     }  

  55.   

  56.     public void setBeanFactory(BeanFactory arg0) throws BeansException {  

  57.         System.out.println("调用BeanFactoryAware的setBeanFactory()...");  

  58.     }  

  59. }  


2.测试程序 

Java代码  收藏代码

  1. package com.spring.springtest;  

  2.   

  3. import junit.framework.TestCase;  

  4.   

  5. import org.springframework.beans.factory.BeanFactory;  

  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  

  7.   

  8. import com.spring.lifecycle.HelloWorld;  

  9.   

  10. public class TestLifeCycle extends TestCase {  

  11.     private BeanFactory bf;  

  12.   

  13.     protected void setUp() throws Exception {  

  14.         bf = new ClassPathXmlApplicationContext("applicationContext.xml");  

  15.     }  

  16.   

  17.     public void testLifeCycle() throws Exception {  

  18.         HelloWorld hello = (HelloWorld) bf.getBean("helloWorld");  

  19.         assertEquals("hello world!", hello.getHello());  

  20.         hello.destroy();  

  21.     }  

  22. }  


3.applicationContext.xml 

Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  

  3. <beans>  

  4.     <bean id="helloWorld" class="com.spring.lifecycle.HelloWorld"  

  5.         init-method="customInit" destroy-method="customDestroy">  

  6.         <property name="hello" value="hello world!"></property>  

  7.     </bean>  

  8. </beans>  


4.运行结果: 

引用

调用setHello()... 
调用BeanNameAware的setBeanName()... 
调用BeanFactoryAware的setBeanFactory()... 
调用InitializingBean的afterPropertiesSet()... 
调用customInit()... 
调用DisposableBean的destory()...


转载于:https://my.oschina.net/u/1472917/blog/399951

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值