ITeye博客

Spring中Bean的初始化过程

gaolixu · 2009-06-03 16:47 · Spring · junit · Spring · Go · XML · Bean · Markdown

在传统的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. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.beans.factory.BeanFactoryAware;
  5. import org.springframework.beans.factory.BeanNameAware;
  6. import org.springframework.beans.factory.DisposableBean;
  7. import org.springframework.beans.factory.InitializingBean;
  8. import org.springframework.beans.factory.config.BeanPostProcessor;
  9. public class HelloWorld implements BeanNameAware,BeanFactoryAware, BeanPostProcessor,InitializingBean,DisposableBean{
  10. private String hello;
  11. public void setBeanName(String arg0) {
  12. System.out.println("调用BeanNameAware的setBeanName()..." );
  13. }
  14. public String getHello() {
  15. return hello;
  16. }
  17. public void setHello(String hello) {
  18. this.hello = hello;
  19. System.out.println("调用setHello()..." );
  20. }
  21. public void customInit() {
  22. System.out.println("调用customInit()...");
  23. }
  24. public void customDestroy() {
  25. System.out.println("调用customDestroy()...");
  26. }
  27. public Object postProcessAfterInitialization(Object arg0, String arg1)
  28. throws BeansException {
  29. System.out.println("调用BeanPostProcessor的postProcessAfterInitialization()...");
  30. return null;
  31. }
  32. public Object postProcessBeforeInitialization(Object arg0, String arg1)
  33. throws BeansException {
  34. System.out.println("调用BeanPostProcessor的postProcessBeforeInitialization()...");
  35. return null;
  36. }
  37. public void destroy() throws Exception {
  38. System.out.println("调用DisposableBean的destory()...");
  39. }
  40. public void afterPropertiesSet() throws Exception {
  41. System.out.println("调用InitializingBean的afterPropertiesSet()...");
  42. }
  43. public void setBeanFactory(BeanFactory arg0) throws BeansException {
  44. System.out.println("调用BeanFactoryAware的setBeanFactory()...");
  45. }
  46. }
package com.spring.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;

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

	private String hello;

	public void setBeanName(String arg0) {
		System.out.println("调用BeanNameAware的setBeanName()..." );
	}

	public String getHello() {
		return hello;
	}

	public void setHello(String hello) {
		this.hello = hello;
		System.out.println("调用setHello()..." );
	}

	public void customInit() {
		System.out.println("调用customInit()...");
	}

	public void customDestroy() {
		System.out.println("调用customDestroy()...");
	}

	public Object postProcessAfterInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("调用BeanPostProcessor的postProcessAfterInitialization()...");
		return null;
	}

	public Object postProcessBeforeInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("调用BeanPostProcessor的postProcessBeforeInitialization()...");
		return null;
	}

	public void destroy() throws Exception {
		System.out.println("调用DisposableBean的destory()...");
	}

	public void afterPropertiesSet() throws Exception {
		System.out.println("调用InitializingBean的afterPropertiesSet()...");
	}

	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("调用BeanFactoryAware的setBeanFactory()...");
	}
}

2.测试程序

Java代码 复制代码

  1. package com.spring.springtest;
  2. import junit.framework.TestCase;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.spring.lifecycle.HelloWorld;
  6. public class TestLifeCycle extends TestCase {
  7. private BeanFactory bf;
  8. protected void setUp() throws Exception {
  9. bf = new ClassPathXmlApplicationContext("applicationContext.xml");
  10. }
  11. public void testLifeCycle() throws Exception {
  12. HelloWorld hello = (HelloWorld) bf.getBean("helloWorld");
  13. assertEquals("hello world!", hello.getHello());
  14. hello.destroy();
  15. }
  16. }
package com.spring.springtest;

import junit.framework.TestCase;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.lifecycle.HelloWorld;

public class TestLifeCycle extends TestCase {
	private BeanFactory bf;

	protected void setUp() throws Exception {
		bf = new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	public void testLifeCycle() throws Exception {
		HelloWorld hello = (HelloWorld) bf.getBean("helloWorld");
		assertEquals("hello world!", hello.getHello());
		hello.destroy();
	}
}

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>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="helloWorld" class="com.spring.lifecycle.HelloWorld"
		init-method="customInit" destroy-method="customDestroy">
		<property name="hello" value="hello world!"></property>
	</bean>
</beans>

4.运行结果:

引用

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

2.

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="life" name="life_name" class="com.open.bean.Life"
        init-method="init">
        <property name="msg" value="lifexxxx"/>
    </bean>   
    <bean id="processor" class="com.open.bean.BeanPostProcessorImp"/>
    <bean id="counter" class="com.open.bean.BeanCounter"/>
</beans>测试类 package com.open.bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cx=
            new ClassPathXmlApplicationContext("bean.xml");
         Life life=(Life)cx.getBean("life");    
    }
}    输出结果 类的数量=3
msg=null
构造函数
setBeanName
setBeanFactory
postProcessBeforeInitialization
afterPropertiesSet
初始化
postProcessAfterInitialization解释:
调用BeanFactoryPostProcessor接口的postProcessBeanFactory方法
-->实例化,调用构造函数-->设置属性值
-->调用BeanNameAware的setBeanName()方法
-->调用BeanFactoryAware接口的setBeanFactory
-->调用BeanPostProcessor接口的postProcessBeforeInitialization()
-->调用InitializingBean接口的afterPropertiesSet()
-->调用bean.xml中制定的init-method指定init()方法
-->调用BeanPostProcessor接口的postProcessAfterInitialization()
容器关闭
-->DisposableBean接口的destroy()
-->调用bean.xml中制定的destroy-method指定的go()方法

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。