您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。