1 16 17 package org.springframework.beans.factory.config; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Proxy ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.springframework.beans.SimpleTypeConverter; 28 import org.springframework.beans.TypeConverter; 29 import org.springframework.beans.factory.BeanFactory; 30 import org.springframework.beans.factory.BeanFactoryAware; 31 import org.springframework.beans.factory.DisposableBean; 32 import org.springframework.beans.factory.FactoryBean; 33 import org.springframework.beans.factory.FactoryBeanNotInitializedException; 34 import org.springframework.beans.factory.InitializingBean; 35 36 56 public abstract class AbstractFactoryBean 57 implements FactoryBean, BeanFactoryAware, InitializingBean, DisposableBean { 58 59 60 protected final Log logger = LogFactory.getLog(getClass()); 61 62 private boolean singleton = true; 63 64 private BeanFactory beanFactory; 65 66 private boolean initialized = false; 67 68 private Object singletonInstance; 69 70 private Object earlySingletonInstance; 71 72 73 77 public void setSingleton(boolean singleton) { 78 this.singleton = singleton; 79 } 80 81 public boolean isSingleton() { 82 return this.singleton; 83 } 84 85 public void setBeanFactory(BeanFactory beanFactory) { 86 this.beanFactory = beanFactory; 87 } 88 89 92 protected BeanFactory getBeanFactory() { 93 return this.beanFactory; 94 } 95 96 104 protected TypeConverter getBeanTypeConverter() { 105 BeanFactory beanFactory = getBeanFactory(); 106 if (beanFactory instanceof ConfigurableBeanFactory) { 107 return ((ConfigurableBeanFactory) beanFactory).getTypeConverter(); 108 } 109 else { 110 return new SimpleTypeConverter(); 111 } 112 } 113 114 117 public void afterPropertiesSet() throws Exception { 118 if (isSingleton()) { 119 this.initialized = true; 120 this.singletonInstance = createInstance(); 121 this.earlySingletonInstance = null; 122 } 123 } 124 125 126 131 public final Object getObject() throws Exception { 132 if (isSingleton()) { 133 return (this.initialized ? this.singletonInstance : getEarlySingletonInstance()); 134 } 135 else { 136 return createInstance(); 137 } 138 } 139 140 144 private Object getEarlySingletonInstance() throws Exception { 145 Class [] ifcs = getEarlySingletonInterfaces(); 146 if (ifcs == null) { 147 throw new FactoryBeanNotInitializedException( 148 getClass().getName() + " does not support circular references"); 149 } 150 if (this.earlySingletonInstance == null) { 151 this.earlySingletonInstance = Proxy.newProxyInstance(getClass().getClassLoader(), ifcs, 152 new InvocationHandler () { 153 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 154 try { 155 return method.invoke(getSingletonInstance(), args); 156 } 157 catch (InvocationTargetException ex) { 158 throw ex.getTargetException(); 159 } 160 } 161 }); 162 } 163 return this.earlySingletonInstance; 164 } 165 166 171 private Object getSingletonInstance() throws IllegalStateException { 172 if (!this.initialized) { 173 throw new IllegalStateException ("Singleton instance not initialized yet"); 174 } 175 return this.singletonInstance; 176 } 177 178 182 public void destroy() throws Exception { 183 if (isSingleton()) { 184 destroyInstance(this.singletonInstance); 185 } 186 } 187 188 189 197 public abstract Class getObjectType(); 198 199 208 protected abstract Object createInstance() throws Exception ; 209 210 222 protected Class [] getEarlySingletonInterfaces() { 223 Class type = getObjectType(); 224 return (type != null && type.isInterface() ? new Class [] {type} : null); 225 } 226 227 236 protected void destroyInstance(Object instance) throws Exception { 237 } 238 239 } 240 | Popular Tags |