您好,登錄后才能下訂單哦!
小編給大家分享一下Java Spring Bean生命周期管理的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
通過以下方式來指定Bean的初始化和銷毀方法, 當Bean為單例時,Bean歸Spring容器管理,Spring容器關閉,就會調用Bean的銷毀方法 當Bean為多例時,Bean不歸Spring容器管理,Spring容器關閉,不會調用Bean的銷毀方法
public class Person { public Person(){ System.out.println("Person 創建了..."); } public void init(){ System.out.println("Person 初始化了..."); } public void destroy(){ System.out.println("Person 被銷毀了..."); } }
import com.dashu.bean.Person; import org.springframework.context.annotation.*; @Configuration public class BeanConfig { @Bean(initMethod = "init",destroyMethod = "destroy") public Person person(){ return new Person(); } }
import com.dashu.bean.Person; import com.dashu.config.BeanConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { //加載配置類獲取容器 AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class); //獲取Bean Person bean = annotationConfigApplicationContext.getBean(Person.class); //關閉容器 annotationConfigApplicationContext.close(); } }
import com.dashu.bean.Person; import org.springframework.context.annotation.*; @Configuration public class BeanConfig { @Scope("prototype") @Bean(initMethod = "init",destroyMethod = "destroy") public Person person(){ return new Person(); } }
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Person implements InitializingBean, DisposableBean { public Person(){ System.out.println("Person 創建了..."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("Person 初始化了..."); } @Override public void destroy() throws Exception { System.out.println("Person 被銷毀了..."); } }
import com.dashu.bean.Person; import org.springframework.context.annotation.*; @Configuration public class BeanConfig { @Bean public Person person(){ return new Person(); } }
@PostConstruct:標注在Bean的初始化方法上 @PreDestroy:標注在Bean的銷毀方法上
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Person { public Person(){ System.out.println("Person 創建了..."); } @PostConstruct public void init(){ System.out.println("Person 初始化了..."); } @PreDestroy public void destroy(){ System.out.println("Person 被銷毀了..."); } }
import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Component public class Person { public Person(){ System.out.println("Person 創建了..."); } @PostConstruct public void init(){ System.out.println("Person 初始化了..."); } @PreDestroy public void destroy(){ System.out.println("Person 被銷毀了..."); } }
import org.springframework.context.annotation.*; @Configuration @ComponentScan({"com.dashu"}) public class BeanConfig { }
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; /** * 后置處理器:初始化前后進行處理工作 */ @Component public class MyBeanPostProcessor implements BeanPostProcessor { /** * 初始化之前工作 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("初始化之前..."+beanName+"=["+bean+"]"); return bean; } /** * 初始化之后工作 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("初始化之后..."+beanName+"=["+bean+"]"); return bean; } }
看完了這篇文章,相信你對“Java Spring Bean生命周期管理的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。