1 5 package com.tcspring; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.springframework.beans.BeansException; 10 import org.springframework.beans.PropertyValues; 11 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; 12 13 import java.beans.PropertyDescriptor ; 14 15 20 public class DistributableBeanPostProcessor implements InstantiationAwareBeanPostProcessor { 21 22 private final transient Log logger = LogFactory.getLog(getClass()); 23 24 private final DistributableBeanFactory factory; 25 26 public DistributableBeanPostProcessor(DistributableBeanFactory factory) { 27 this.factory = factory; 28 } 29 30 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 31 logger.info(factory.getId() + " post processing before initialization " + isDistributed(beanName)); 32 return bean; 33 } 34 35 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 36 logger.info(factory.getId() + " post processing after initialization " + isDistributed(beanName)); 37 if (factory.isDistributedSingleton(beanName)) { 38 ComplexBeanId beanId = new ComplexBeanId(beanName); 39 BeanContainer container = factory.getBeanContainer(beanId); 40 if (container == null) { 41 logger.info(factory.getId() + " distributing new bean " + beanName); 42 factory.putBeanContainer(beanId, new BeanContainer(bean, true)); 43 } else { 44 logger.info(factory.getId() + " initializing existing bean " + beanName); 45 factory.initializeBean(beanId, bean, container); 46 container.setInitialized(true); 47 return container.getBean(); 48 } 49 } 50 return bean; 51 } 52 53 public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { 54 logger.info(factory.getId() + " post processing before instantiation " + isDistributed(beanName)); 55 return null; 56 } 57 58 public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { 59 logger.info(factory.getId() + " post processing after instantiation " + isDistributed(beanName)); 60 return true; 61 } 62 63 public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor [] pds, Object bean, 64 String beanName) throws BeansException { 65 logger.info(factory.getId() + " post processing property values " + isDistributed(beanName)); 66 return pvs; 67 } 68 69 private String isDistributed(String beanName) { 70 return (factory.isDistributedSingleton(beanName) ? "distributed" : "local") + " bean " + beanName; 71 } 72 73 } 74 | Popular Tags |