Spring-ApplicationContextAware

ApplicationContextAware接口

如果在配置文件中的Bean如果实现接口ApplicationContextAware,那么在Bean初始化结束后会调用接口ApplicationContextAware中的方法setApplicationContext(ApplicationContext arg0),通过该方法,我们能获取上下文ApplicationContext 对象,通过该对象可以获取任何已经初始化的bean对象。

代码

spring 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        	http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
	default-lazy-init="false">

	<!--  <bean id="myJavaBean" class=" cn.com.init.MyJavaBean" init-method="initMethod">
		<property name="desc" value="原始的描述信息" />
		<property name="remark" value="原始的备注信息" />
	</bean>

	<bean id="myBeanPostProcessor" class=" cn.com.init.MyBeanPostProcessor" />
	<bean id="myBeanFactoryPostProcessor" class=" cn.com.init.MyBeanFactoryPostProcessor" />  -->

 <bean id="hello" class="cn.com.init.HelloWorld" init-method="init" >
<property name="hello" value="hello" />
</bean> 
</beans>
Bean类

package cn.com.init;

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.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

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

	public HelloWorld() {
		System.out.println("调用HelloWorld构造器...");
	}

	private String hello;

	public String getHello() {
		return hello;
	}

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

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("调用BeanPostProcessor的postProcessBeforeInitialization()...");
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("调用BeanPostProcessor的postProcessAfterInitialization()...");
		return bean;
	}

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

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory paramConfigurableListableBeanFactory)
			throws BeansException {
		System.out.println("调用BeanFactoryPostProcessor的postProcessBeanFactory()...");

	}

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

	@Override
	public void setBeanName(String paramString) {
		System.out.println("调用BeanNameAware.setBeanName");

	}

	@Override
	public void setBeanFactory(BeanFactory paramBeanFactory) throws BeansException {
		System.out.println("调用BeanFactoryAware.setBeanFactory");

	}

	@Override
	public void destroy() throws Exception {
		System.out.println("DisposableBean 接口 destroy方法");

	}

	public void init() throws Exception {
		System.out.println("HelloWorld类init 方法");

	}

	public void print(){
		System.out.println("I'm HelloWorld's method print()");
	}
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		HelloWorld hellobean = (HelloWorld)arg0.getBean("hello");
		hellobean.print();
	}
}

测试类:

package mybatis;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.init.HelloWorld;
import junit.framework.TestCase;

public class BeanPostPorcessorTest extends TestCase {
	private ClassPathXmlApplicationContext context;  
    protected void setUp() throws Exception {  
        super.setUp();  
        String[] paths = {"classpath*:spring-text.xml"};  
  
        context = new ClassPathXmlApplicationContext(paths);  
          
    }  
	    public void testBeanFactoryPostProcessor()  
	    {  
	    	 HelloWorld bean = (HelloWorld) context.getBean("hello");  
	    	 System.out.println(bean);
	    	 context.getBeanFactory().destroySingletons();
//	    	 MyJavaBean bean = (MyJavaBean) context.getBean("myJavaBean");  
//	         System.out.println("===============下面输出结果============");  
//	         System.out.println("描述:" + bean.getDesc());  
//	         System.out.println("备注:" + bean.getRemark());  
	    }  
}

执行结果:

调用HelloWorld构造器...
调用setHello()...
调用BeanNameAware.setBeanName
调用BeanFactoryAware.setBeanFactory
I'm HelloWorld's method print()
调用InitializingBean的afterPropertiesSet()...
HelloWorld类init 方法
调用BeanFactoryPostProcessor的postProcessBeanFactory()..

HelloWorld [hello=hello]

DisposableBean 接口 destroy方法

由执行结果可以看到在Bean初始化,实现接口ApplicationContextAware的方法

setApplicationContext<span style="font-family: Arial, Helvetica, sans-serif;">(ApplicationContext arg0)能够获取到我们需要已经初始化后的bean对象。</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值