1 16 17 package org.springframework.beans.factory.wiring; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import org.springframework.beans.factory.BeanFactory; 23 import org.springframework.beans.factory.BeanFactoryAware; 24 import org.springframework.beans.factory.DisposableBean; 25 import org.springframework.beans.factory.InitializingBean; 26 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 27 import org.springframework.util.Assert; 28 29 47 public abstract class BeanConfigurerSupport implements BeanFactoryAware, InitializingBean, DisposableBean { 48 49 50 protected Log logger = LogFactory.getLog(getClass()); 51 52 private BeanWiringInfoResolver beanWiringInfoResolver; 53 54 private AutowireCapableBeanFactory beanFactory; 55 56 57 66 public void setBeanWiringInfoResolver(BeanWiringInfoResolver beanWiringInfoResolver) { 67 Assert.notNull(beanWiringInfoResolver, "'beanWiringInfoResolver' cannot be null."); 68 this.beanWiringInfoResolver = beanWiringInfoResolver; 69 } 70 71 76 public void setBeanFactory(BeanFactory beanFactory) { 77 if (!(beanFactory instanceof AutowireCapableBeanFactory)) { 78 throw new IllegalArgumentException ( 79 "Bean configurer aspect needs to run in an AutowireCapableBeanFactory, not in [" + beanFactory + "]"); 80 } 81 this.beanFactory = (AutowireCapableBeanFactory) beanFactory; 82 } 83 84 88 public void afterPropertiesSet() throws Exception { 89 if (this.beanWiringInfoResolver == null) { 90 this.beanWiringInfoResolver = new ClassNameBeanWiringInfoResolver(); 91 } 92 } 93 94 98 public void destroy() { 99 this.beanFactory = null; 100 this.beanWiringInfoResolver = null; 101 } 102 103 104 111 protected void configureBean(Object beanInstance) { 112 if (this.beanWiringInfoResolver == null) { 113 if(logger.isWarnEnabled()) { 114 logger.warn("[" + getClass().getName() + "] has not been configured by Spring " + 115 "and is unable to configure bean instances. Object with identity " + 116 "hashcode " + System.identityHashCode(beanInstance) + " has not been configured: " + 117 "Make sure this configurer runs in a Spring container. " + 118 "For example, add it to a Spring application context as an XML bean definition."); 119 } 120 return; 121 } 122 123 BeanWiringInfo bwi = this.beanWiringInfoResolver.resolveWiringInfo(beanInstance); 124 if (bwi == null) { 125 return; 127 } 128 129 if (this.beanFactory == null) { 130 if(logger.isWarnEnabled()) { 131 logger.warn("BeanFactory has not been set on [" + getClass().getName() + "]: " + 132 "Make sure this configurer runs in a Spring container. " + 133 "For example, add it to a Spring application context as an XML bean definition."); 134 } 135 return; 136 } 137 138 if (bwi.indicatesAutowiring()) { 139 this.beanFactory.autowireBeanProperties(beanInstance, bwi.getAutowireMode(), bwi.getDependencyCheck()); 141 } 142 else { 143 this.beanFactory.applyBeanPropertyValues(beanInstance, bwi.getBeanName()); 145 } 146 } 147 148 } 149 | Popular Tags |