1 16 17 package org.springframework.context.support; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import org.springframework.beans.BeansException; 23 import org.springframework.beans.factory.config.BeanPostProcessor; 24 import org.springframework.context.ApplicationContext; 25 import org.springframework.context.ApplicationContextAware; 26 import org.springframework.context.ApplicationEventPublisherAware; 27 import org.springframework.context.MessageSourceAware; 28 import org.springframework.context.ResourceLoaderAware; 29 30 47 public class ApplicationContextAwareProcessor implements BeanPostProcessor { 48 49 protected final Log logger = LogFactory.getLog(getClass()); 50 51 private final ApplicationContext applicationContext; 52 53 54 57 public ApplicationContextAwareProcessor(ApplicationContext applicationContext) { 58 this.applicationContext = applicationContext; 59 } 60 61 62 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 63 if (bean instanceof ResourceLoaderAware) { 64 if (logger.isTraceEnabled()) { 65 logger.trace("Invoking setResourceLoader on ResourceLoaderAware bean '" + beanName + "'"); 66 } 67 ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); 68 } 69 if (bean instanceof ApplicationEventPublisherAware) { 70 if (logger.isTraceEnabled()) { 71 logger.trace("Invoking setApplicationEventPublisher on ApplicationEventPublisherAware bean '" + 72 beanName + "'"); 73 } 74 ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); 75 } 76 if (bean instanceof MessageSourceAware) { 77 if (logger.isTraceEnabled()) { 78 logger.trace("Invoking setMessageSource on MessageSourceAware bean '" + beanName + "'"); 79 } 80 ((MessageSourceAware) bean).setMessageSource(this.applicationContext); 81 } 82 if (bean instanceof ApplicationContextAware) { 83 if (logger.isTraceEnabled()) { 84 logger.trace("Invoking setApplicationContext on ApplicationContextAware bean '" + beanName + "'"); 85 } 86 ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); 87 } 88 return bean; 89 } 90 91 public Object postProcessAfterInitialization(Object bean, String name) { 92 return bean; 93 } 94 95 } 96 | Popular Tags |