Bootstrap

spring bean示例

Spring装配Bean的过程    
1. 实例化;  
2. 设置属性值;  
3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;  
4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;  
5. 如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext  
6. 调用BeanPostProcessor的预先初始化方法;  
7. 调用InitializingBean的afterPropertiesSet()方法;  
8. 调用定制init-method方法;  
9. 调用BeanPostProcessor的后初始化方法;  


Spring容器关闭过程    
1. 调用DisposableBean的destroy();  

2. 调用定制的destroy-method方法;


下面是一组例子:


BeanNameAware

public class TestNameAwareBean implements BeanNameAware {

	private String beanName = null;

	@Override
	public void setBeanName(String arg0) {
		this.beanName = arg0;
	}

	@Override
	public String toString() {
		return "TestNameAwareBean [beanName=" + beanName + "]";
	}

}

BeanFactoryAware

public class TestFactoryAwareBean implements BeanFactoryAware {

	private BeanFactory beanFactory = null;

	@Override
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		this.beanFactory = arg0;
	}

	public BeanFactory getBeanFactory() {
		return beanFactory;
	}

	@Override
	public String toString() {
		return "TestFactoryAwareBean [beanFactory=" + beanFactory.toString()
				+ "]";
	}

}

ApplicationContextAware

public class TestApplicationContextAwareBean implements ApplicationContextAware {

	private ApplicationContext context = null;

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.context = arg0;
	}

	@Override
	public String toString() {
		return "TestApplicationContextAwareBean [context=" + context.toString()
				+ "]";
	}

}

BeanPostProcessor

public class TestPostProcessorBean implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("after: " + beanName);
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("before: " + beanName);
		return bean;
	}

}

InitializingBean

public class TestInitializingBean implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("TestInitializingBean afterPropertiesSet");
	}

}

DisposableBean

public class TestDisposableBean implements DisposableBean{

	@Override
	public void destroy() throws Exception {
		System.out.println("TestDisposableBean destroy");
	}

}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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">

	<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
	<!-- 
	<bean
		class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	 -->

	<bean id="testNameAwareBean" class="com.test.spring.bean.TestNameAwareBean">
	</bean>

	<bean id="testFactoryAwareBean" class="com.test.spring.bean.TestFactoryAwareBean">
	</bean>

	<bean id="testApplicationContextAwareBean" class="com.test.spring.bean.TestApplicationContextAwareBean">
	</bean>

	<bean id="testPostProcessorBean" class="com.test.spring.bean.TestPostProcessorBean">
	</bean>

	<bean id="testInitializingBean" class="com.test.spring.bean.TestInitializingBean">
	</bean>

	<bean id="testDisposableBean" class="com.test.spring.bean.TestDisposableBean">
	</bean>

</beans>


测试程序
public class TestSpringBean {

	public static void main(String[] args) {
		String[] locations = { "applicationContext.xml" };
		ApplicationContext context = new ClassPathXmlApplicationContext(
				locations);

		// BeanNameAware
		TestNameAwareBean testNameAwareBean = (TestNameAwareBean) context
				.getBean("testNameAwareBean");
		System.out.println(testNameAwareBean);

		// BeanFactoryAware
		TestFactoryAwareBean testFactoryAwareBean = (TestFactoryAwareBean) context
				.getBean("testFactoryAwareBean");
		System.out.println(testFactoryAwareBean);

		// ApplicationContextAware
		TestApplicationContextAwareBean testApplicationContextAwareBean = (TestApplicationContextAwareBean) context
				.getBean("testApplicationContextAwareBean");
		System.out.println(testApplicationContextAwareBean);

		// BeanPostProcessor
		TestPostProcessorBean testPostProcessorBean = (TestPostProcessorBean) context
				.getBean("testPostProcessorBean");
		System.out.println(testPostProcessorBean);

		// InitializingBean
		TestInitializingBean testInitializingBean = (TestInitializingBean) context
				.getBean("testInitializingBean");
		System.out.println(testInitializingBean);

		// DisposableBean
		TestDisposableBean testDisposableBean = (TestDisposableBean) context
				.getBean("testDisposableBean");
		System.out.println(testDisposableBean);

	}

}

结果

2015-3-5 14:13:27 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1749757: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1749757]; startup date [Thu Mar 05 14:13:27 CST 2015]; root of context hierarchy
2015-3-5 14:13:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2015-3-5 14:13:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1749757]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18235ed
2015-3-5 14:13:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18235ed: defining beans [testNameAwareBean,testFactoryAwareBean,testApplicationContextAwareBean,testPostProcessorBean,testInitializingBean,testDisposableBean]; root of factory hierarchy
before: testNameAwareBean
after: testNameAwareBean
before: testFactoryAwareBean
after: testFactoryAwareBean
before: testApplicationContextAwareBean
after: testApplicationContextAwareBean
before: testInitializingBean
TestInitializingBean afterPropertiesSet
after: testInitializingBean
before: testDisposableBean
after: testDisposableBean
TestNameAwareBean [beanName=testNameAwareBean]
TestFactoryAwareBean [beanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@18235ed: defining beans [testNameAwareBean,testFactoryAwareBean,testApplicationContextAwareBean,testPostProcessorBean,testInitializingBean,testDisposableBean]; root of factory hierarchy]
TestApplicationContextAwareBean [context=org.springframework.context.support.ClassPathXmlApplicationContext@1749757: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1749757]; startup date [Thu Mar 05 14:13:27 CST 2015]; root of context hierarchy]
com.test.spring.bean.TestPostProcessorBean@14e8cee
com.test.spring.bean.TestInitializingBean@67064
com.test.spring.bean.TestDisposableBean@bcda2d


可见BeanPostProcessor的使用还是要慎重啊,侵入性太强了。

另外dispose也不是很好演示效果。

init-method和destroy-method方法就是在xml里配置一下某个类的方法就可以了。

;