1 16 17 package org.springframework.beans.factory; 18 19 import org.springframework.beans.BeansException; 20 import org.springframework.beans.factory.config.BeanPostProcessor; 21 22 29 public class LifecycleBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean { 30 31 protected String beanName; 32 33 protected boolean initMethodDeclared = false; 34 35 protected BeanFactory owningFactory; 36 37 protected boolean postProcessedBeforeInit; 38 39 protected boolean inited; 40 protected boolean initedViaDeclaredInitMethod; 41 42 protected boolean postProcessedAfterInit; 43 44 protected boolean destroyed; 45 46 public boolean isInitMethodDeclared() { 47 return initMethodDeclared; 48 } 49 public void setInitMethodDeclared(boolean initMethodDeclared) { 50 this.initMethodDeclared = initMethodDeclared; 51 } 52 53 public void setBeanName(String name) { 54 this.beanName = name; 55 } 56 public String getBeanName() { 57 return beanName; 58 } 59 60 public void setBeanFactory(BeanFactory beanFactory) { 61 this.owningFactory = beanFactory; 62 } 63 64 public void postProcessBeforeInit() { 65 if (this.inited || this.initedViaDeclaredInitMethod) { 66 throw new RuntimeException ("Factory called postProcessBeforeInit after afterPropertiesSet"); 67 } 68 if (this.postProcessedBeforeInit) { 69 throw new RuntimeException ("Factory called postProcessBeforeInit twice"); 70 } 71 this.postProcessedBeforeInit = true; 72 } 73 74 public void afterPropertiesSet() { 75 if (this.owningFactory == null) { 76 throw new RuntimeException ("Factory didn't call setBeanFactory before afterPropertiesSet on lifecycle bean"); 77 } 78 if (!this.postProcessedBeforeInit) { 79 throw new RuntimeException ("Factory didn't call postProcessBeforeInit before afterPropertiesSet on lifecycle bean"); 80 } 81 if (this.initedViaDeclaredInitMethod) { 82 throw new RuntimeException ("Factory initialized via declared init method before initializing via afterPropertiesSet"); 83 } 84 if (this.inited) { 85 throw new RuntimeException ("Factory called afterPropertiesSet twice"); 86 } 87 this.inited = true; 88 } 89 90 public void declaredInitMethod() { 91 92 if (!this.inited) { 93 throw new RuntimeException ("Factory didn't call afterPropertiesSet before declared init method"); 94 } 95 96 if (this.initedViaDeclaredInitMethod) { 97 throw new RuntimeException ("Factory called declared init method twice"); 98 } 99 this.initedViaDeclaredInitMethod = true; 100 } 101 102 public void postProcessAfterInit() { 103 if (!this.inited) { 104 throw new RuntimeException ("Factory called postProcessAfterInit before afterPropertiesSet"); 105 } 106 if (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) { 107 throw new RuntimeException ("Factory called postProcessAfterInit before calling declared init method"); 108 } 109 if (this.postProcessedAfterInit) { 110 throw new RuntimeException ("Factory called postProcessAfterInit twice"); 111 } 112 this.postProcessedAfterInit = true; 113 } 114 115 119 public void businessMethod() { 120 if (!this.inited || (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) 121 || !this.postProcessedAfterInit) { 122 throw new RuntimeException ("Factory didn't initialize lifecycle object correctly"); 123 } 124 } 125 126 public void destroy() { 127 if (this.destroyed) { 128 throw new IllegalStateException ("Already destroyed"); 129 } 130 this.destroyed = true; 131 } 132 133 public boolean isDestroyed() { 134 return destroyed; 135 } 136 137 138 public static class PostProcessor implements BeanPostProcessor { 139 140 public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { 141 if (bean instanceof LifecycleBean) { 142 ((LifecycleBean) bean).postProcessBeforeInit(); 143 } 144 return bean; 145 } 146 147 public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { 148 if (bean instanceof LifecycleBean) { 149 ((LifecycleBean) bean).postProcessAfterInit(); 150 } 151 return bean; 152 } 153 } 154 } 155 | Popular Tags |