1. 程式人生 > >Spring啟動常用的幾個介面

Spring啟動常用的幾個介面

1.ApplicationContextAware

通過實現ApplicationContextAware介面,在ApplicationContext執行的時候被通知並注入ApplicationContext上下文。

package cn.slimsmart.spring.demo.Interface;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

//實現該介面,在ApplicationContext執行的時候被通知並注入ApplicationContext
@Component
public class ApplicationContextAwareTest implements ApplicationContextAware {
	
	private Log log = LogFactory.getLog(getClass());

	public void setApplicationContext(ApplicationContext context) throws BeansException {
		log.info("-----------ApplicationContext is runing");
	}
}
2.BeanNameAware

通過實現BeanNameAware介面可以知道在BeanFactory中設定的名字時可以實現該介面。

package cn.slimsmart.spring.demo.Interface;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;

//如果Bean想知道在BeanFactory中設定的名字時可以實現該介面
@Component
public class BeanNameAwareTest implements BeanNameAware{
	
	private Log log = LogFactory.getLog(getClass());
	
	private String beanName;

	public void setBeanName(String beanName) {
		log.info("--------setBeanName:"+beanName);
		this.beanName = beanName;
	}
	
	public String getBeanName() {
        return beanName;
    }
}
3.InitializingBean

通過實現InitializingBean介面可以在BeanFactory 設定所有的屬性後作出進一步的反應可以實現該介面。

package cn.slimsmart.spring.demo.Interface;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

//如果期望在BeanFactory 設定所有的屬性後作出進一步的反應可以實現該介面
@Component
public class InitializingBeanTest  implements InitializingBean, BeanNameAware {
	
	private Log log = LogFactory.getLog(getClass());
	
	private String beanName;

	public void setBeanName(String beanName) {
		log.info("--------setBeanName:"+beanName);
		this.beanName = beanName;
	}

	public void afterPropertiesSet() throws Exception {
		log.info("--------Bean的屬性都被設定完成:"+beanName);
	}
}
4.BeanPostProcessor

通過實現BeanPostProcessor介面允許使用者對新建的Bean進行修改。

package cn.slimsmart.spring.demo.Interface;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

//BeanPostProcessor是BeanFactory的鉤子允許使用者對新建的Bean進行修改
@Component
public class BeanPostProcessorTest implements BeanPostProcessor {
	
	private Log log = LogFactory.getLog(getClass());

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		log.info("--------初始化之後呼叫:bean="+bean+",beanName="+beanName);
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		log.info("--------初始化之前呼叫:bean="+bean+",beanName="+beanName);
		return bean;
	}

}
執行結果如下:
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.ApplicationContextAwareTest setApplicationContext
資訊: -----------ApplicationContext is runing
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessBeforeInitialization
資訊: --------初始化之前呼叫:bean[email protected]2d3ea433,beanName=applicationContextAwareTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessAfterInitialization
資訊: --------初始化之後呼叫:bean[email protected]2d3ea433,beanName=applicationContextAwareTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanNameAwareTest setBeanName
資訊: --------setBeanName:beanNameAwareTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessBeforeInitialization
資訊: --------初始化之前呼叫:bean=cn.slimsmart.spring.demo.Interface.BeanNam[email protected],beanName=beanNameAwareTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessAfterInitialization
資訊: --------初始化之後呼叫:[email protected]a6,beanName=beanNameAwareTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.InitializingBeanTest setBeanName
資訊: --------setBeanName:initializingBeanTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessBeforeInitialization
資訊: --------初始化之前呼叫:[email protected]f624b,beanName=initializingBeanTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.InitializingBeanTest afterPropertiesSet
資訊: --------Bean的屬性都被設定完成:initializingBeanTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessAfterInitialization
資訊: --------初始化之後呼叫:[email protected]f624b,beanName=initializingBeanTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessBeforeInitialization
資訊: --------初始化之前呼叫:[email protected],beanName=cn.slimsmart.spring.demo.SpringTest
一月 31, 2015 10:41:29 下午 cn.slimsmart.spring.demo.Interface.BeanPostProcessorTest postProcessAfterInitialization
資訊: --------初始化之後呼叫:[email protected],beanName=cn.slimsmart.spring.demo.SpringTest
啟動服務