- spring 中初始化和销毁有三种办法
1、实现接口 DisposableBean ,initilzingBean
2、使用注解 @PostConstructor, @PreDestory
3、@Bean(initMethod = "",destroyMethod="")
package com.shanjiancaofu.ad.service.impl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { // initMethod 指定类的初始化方法, destroyMethod 指定类的销毁方法 @Bean(initMethod = "init", destroyMethod = "destroyMethod") User user() { return new User(); } }
User 类
package com.shanjiancaofu.ad.service.impl; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; class User implements InitializingBean, DisposableBean { public String userName; public User() { System.out.println("User constructor"); } public void init() { this.userName = "initName"; System.out.println(" user init. ...."); } public void destroyMethod() { this.userName = "destroyName"; System.out.println(" user destroy. ...."); } @PreDestroy public void preDestroy() { System.out.println("@PreDestroy ..."); } @PostConstruct public void postConstruct() { System.out.println(" @PostConstruct. ...."); } @Override public String toString() { return "user:" + this.userName; } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean afterPropertiesSet"); } @Override public void destroy() throws Exception { System.out.println("DisposableBean destroy"); } }
启动spring 应用 控制台输出:
User constructor
@PostConstruct. ....
InitializingBean afterPropertiesSet
user init. ....
关闭spring 应用 控制台输出:
@PreDestroy ...
DisposableBean destroy
user destroy. ....
- 获取spring 容器中的bean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext为Null. */ public static void clearHolder() { applicationContext = null; } /** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } /** * 实现DisposableBean接口, 在Context关闭时清理静态变量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * 检查ApplicationContext不为空. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); } }