1. 程式人生 > >不醉也無歸-kleine

不醉也無歸-kleine

Bean的生命週期: Ben的建立初始化銷燬的過程。 容器管理bean的生命週期,我們還可以自定義初始化和銷燬方法,容器在bean進行到當前生命週期的時候來自定義的初始化和銷燬。

構造(物件的建立) 單例項:在容器啟動的時候建立物件; 多例項:在每次獲取的時候建立物件; BeanPostProcessor.postProcessBeforeInitialization 初始化: 物件建立完成,並賦值好,呼叫初始化方法。 BeanPostProcessor. postProcessAfterInitialization 銷燬: 單例項:容器關閉的時候; 多例項:容器不會管理這個bean,容器不會呼叫銷燬方法。

生命週期管理方式: 1) 指定初始化和銷燬方法:指定initMethod和destroyMethod。 import org.springframework.stereotype.Component;

@Component public class Car { public Car() { System.out.println(“Car…Contructor…”); } public void init() { System.out.println(“Car…init…”); } public void destroy() { System.out.println(“Car…destroy…”); } }

@Configuration @ComponentScan(“com.xc.annotation.bean”) public class MyConfig3 { @Scope(“prototype”) @Bean(initMethod=“init”,destroyMethod=“destroy”) public Car car() { return new Car(); } }

2) 通過Bean實現InitializingBean介面(定義初始化邏輯)和DisposableBean(定義銷燬邏輯)。 import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @Component public class Cat implements InitializingBean,DisposableBean { public Cat() { System.out.println(“Cat…Contructor…”); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println(“cat…destroy…”); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println(“cat…afterPropertiesSet…”); } }

3) 可以使用JSR250: @PostConstruct:在bean建立完成並且屬性賦值完成,來執行初始化方法。 @PreDestroy:在容器銷燬bean之前通知我們進行清理工作。 import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class Dog implements ApplicationContextAware{ @Autowired private ApplicationContext applicationContext; public Dog() { System.out.println(“Dog…Constructor…”); } //物件建立並賦值之後呼叫 @PostConstruct public void init() { System.out.println(“Dog…@PostConstruct…”); } //容器移除物件之前 @PreDestroy public void destroy() { System.out.println(“Dog…@PreDestroy…”); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext = applicationContext; } } 4) BeanPostProcessor【interface】:bean的後置處理器: 在bean的初始化前後進行一些處理工作。 postProcessBeforeInitialization:在初始化之前工作; postProcessAfterInitialization:在初始化之後工作。

import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; /**

  • 後置處理器:初始化後進行處理工作。
  • 將後置處理器加入到容器中。
  • @author kleine */ @Component public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(“postProcessAfterInitialization…”+beanName+"–>"+bean); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(“postProcessBeforeInitialization…”+beanName+"–>"+bean); return bean; } }

以上方式的測試類為: import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.xc.annotation.bean.Car; import com.xc.annotation.config.MyConfig3; public class IOCTest_LifeCircle { @SuppressWarnings(“resource”) @Test public void test() { AnnotationConfigApplicationContext atx = new AnnotationConfigApplicationContext(MyConfig3.class); System.out.println(“容器建立完成。。。。”); //Car bean = atx.getBean(Car.class);

	//關閉容器
	atx.close();
}

}

輸出: postProcessBeforeInitialization…myConfig3–>com.xc.annotation.config.MyConfig3EnhancerBySpringCGLIBEnhancerBySpringCGLIB[email protected] postProcessAfterInitialization…myConfig3–>com.xc.annotation.config.MyConfig3EnhancerBySpringCGLIBEnhancerBySpringCGLIB[email protected] Car…Contructor… postProcessBeforeInitialization…car–>[email protected] Car…init… postProcessAfterInitialization…car–>[email protected] Boss… Contruct… postProcessBeforeInitialization…boss–>Boss [[email protected]] postProcessAfterInitialization…boss–>Boss [[email protected]] Cat…Contructor… postProcessBeforeInitialization…cat–>[email protected] cat…afterPropertiesSet… postProcessAfterInitialization…cat–>[email protected] Dog…Constructor… postProcessBeforeInitialization…dog–>[email protected] Dog…@PostConstruct… postProcessAfterInitialization…dog–>[email protected] postProcessBeforeInitialization…red–>[email protected] postProcessAfterInitialization…red–>[email protected] Dog…@PreDestroy… cat…destroy…

補充:BeanPostProcessor的原理(具體檢視原始碼):

遍歷得到容器中所有的BeanPostProcessor,這個執行beforeInitialization一旦返回null,跳出for迴圈,不會執行後面的BeanPostProcessor。

populateBean(beanName,mbd,instanceWrapper):給bean進行屬性賦值。 initializeBean { applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); invokeInitMethods(beanName, wrappedBean, mbd);執行自定義初始化; applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); }

Spring底層對 BeanPostProcessor 的使用: bean賦值,注入其他元件,@Autowired,生命週期註解功能等等都是使用 BeanPostProcessor 完成的。