1 16 17 package org.springframework.beans.factory; 18 19 import org.springframework.beans.BeansException; 20 import org.springframework.beans.TestBean; 21 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 22 23 34 public class DummyFactory 35 implements FactoryBean, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean { 36 37 public static final String SINGLETON_NAME = "Factory singleton"; 38 39 private static boolean prototypeCreated; 40 41 44 public static void reset() { 45 prototypeCreated = false; 46 } 47 48 49 52 private boolean singleton = true; 53 54 private String beanName; 55 56 private AutowireCapableBeanFactory beanFactory; 57 58 private boolean postProcessed; 59 60 private boolean initialized; 61 62 private TestBean testBean; 63 64 private TestBean otherTestBean; 65 66 67 public DummyFactory() { 68 this.testBean = new TestBean(); 69 this.testBean.setName(SINGLETON_NAME); 70 this.testBean.setAge(25); 71 } 72 73 77 public boolean isSingleton() { 78 return this.singleton; 79 } 80 81 84 public void setSingleton(boolean singleton) { 85 this.singleton = singleton; 86 } 87 88 public void setBeanName(String beanName) { 89 this.beanName = beanName; 90 } 91 92 public String getBeanName() { 93 return beanName; 94 } 95 96 public void setBeanFactory(BeanFactory beanFactory) { 97 this.beanFactory = (AutowireCapableBeanFactory) beanFactory; 98 this.beanFactory.applyBeanPostProcessorsBeforeInitialization(this.testBean, this.beanName); 99 } 100 101 public BeanFactory getBeanFactory() { 102 return beanFactory; 103 } 104 105 public void setPostProcessed(boolean postProcessed) { 106 this.postProcessed = postProcessed; 107 } 108 109 public boolean isPostProcessed() { 110 return postProcessed; 111 } 112 113 public void setOtherTestBean(TestBean otherTestBean) { 114 this.otherTestBean = otherTestBean; 115 } 116 117 public TestBean getOtherTestBean() { 118 return otherTestBean; 119 } 120 121 public void afterPropertiesSet() { 122 if (initialized) { 123 throw new RuntimeException ("Cannot call afterPropertiesSet twice on the one bean"); 124 } 125 this.initialized = true; 126 } 127 128 132 public boolean wasInitialized() { 133 return initialized; 134 } 135 136 public static boolean wasPrototypeCreated() { 137 return prototypeCreated; 138 } 139 140 141 146 public Object getObject() throws BeansException { 147 if (isSingleton()) { 148 return this.testBean; 149 } 150 else { 151 TestBean prototype = new TestBean("prototype created at " + System.currentTimeMillis(), 11); 152 if (this.beanFactory != null) { 153 this.beanFactory.applyBeanPostProcessorsBeforeInitialization(prototype, this.beanName); 154 } 155 prototypeCreated = true; 156 return prototype; 157 } 158 } 159 160 public Class getObjectType() { 161 return TestBean.class; 162 } 163 164 165 public void destroy() { 166 if (this.testBean != null) { 167 this.testBean.setName(null); 168 } 169 } 170 171 } 172 | Popular Tags |