本文目的是通过示例告诉大家sprng bean生命周期的控制。
主要通过两个接口InitializingBean、
DisposableBea
,验证bean在容器中初始化以及销毁时候的触发事件。
当然也可以通过配置xml的文件,init-method、
destroy-method等同于上述两个接口
1.InitializingBean and DisposableBean interfaces
package com.javacodegeeks.snippets.enterprise.services; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Employee implements InitializingBean, DisposableBean { private Long id; private String position; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } @Override public String toString() { return "id " + id + " and position " + position; } @PostConstruct public void initIt() throws Exception { System.out.println("Init method after properties are set : " + id + " " + position); } @PreDestroy public void cleanUp() throws Exception { System.out.println("Spring Clean Up! Employee is cleaned up now."); } @Override public void destroy() throws Exception { System.out.println("I am in destroy... "); } @Override public void afterPropertiesSet() throws Exception { System.out.println("I am in afterPropertiesSet... "); } }
2.Use of init-method and destroy-method attributes in bean definition
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <bean id="employeeBean" class="com.javacodegeeks.snippets.enterprise.services.Employee" init-method="initIt" destroy-method="cleanUp"> <property name="id" value="123"/> <property name="position" value="marketing"/> </bean> </beans>
3.@PostConstruct and @PreDestroy annotations
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <context:annotation-config /> <bean id="employeeBean" class="com.javacodegeeks.snippets.enterprise.services.Employee"> <property name="id" value="123"/> <property name="position" value="marketing"/> </bean> </beans>
4.OutPut
相关推荐
bean的生命周期注解,以及各个bean注入注解之间的区别。。。。
弹簧生命周期 一个用于试验 spring 生命周期的小示例应用程序
- Bean lifecycle interfaces - Customizing beans using BeanPostProcessors and BeanFactoryPostProcessors - Bean definition inheritance - JSR 250's and 330's annotations for dependency injection - ...
3.5.1. Lifecycle接口 3.5.1.1. 初始化回调 3.5.1.2. 析构回调 3.5.2. 了解自己 3.5.2.1. BeanFactoryAware 3.5.2.2. BeanNameAware 3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7....
PRODUCT QUALITY LIFECYCLE IMPLEMENTATION (PQLI) Control Strategy - Model and Concepts -完整英文电子版(10页).zip
3.5.1. Lifecycle接口 3.5.2. 了解自己 3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 ...
Flutter的flutter_plugin_android_lifecycle插件是一个Flutter插件,用于访问Android Lifecycle objects。它可以帮助其他Flutter插件在Android上更好地集成。 要使用flutter_plugin_android_lifecycle插件,需要将...
[the lifecycle of an issue]: https://github.com/spring-projects/spring-framework/wiki/The-Lifecycle-of-an-Issue [spring-framework-issues]: ...
androidx-lifecycle-common-2.1.0.jar
3.5.1. Lifecycle接口 3.5.2. 了解自己 3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 ...
第二十章:Spring 应用上下文生命周期小马哥 · mercyblitzSpring 应用上下文生命周期Spring 应用上下文启动准备阶段BeanFacto
Lifecycle ListableBeanFactory ListenerExecutionFailedException ListenerSessionManager ListFactoryBean LoadTimeWeaver LobCreator LobCreatorUtils LobHandler LobRetrievalFailureException ...
Dell Lifecycle Controller 是一种先进的嵌入式系统管理技术 , 借助这种技术 , 可以通过 integrated Dell Remote Access Controller (iDRAC) 来实现远程服务器管理。在使用 Lifecycle Controller 的情况下 ...
SpringDemo:Java集合,Lambda表达式,Stream API和Spring Lifecycle方法的演示
androidx-lifecycle-livedata-2.0.0.aar
androidx-lifecycle-livedata-core-2.0.0.aar
Agile Product Lifecycle Management AdministratGuide
PLM资料。英文的Product Lifecycle Management产品生命周期的教材
PRODUCT QUALITY LIFECYCLE IMPLEMENTATION (PQLI) Control Strategy - Model and Concepts -完整英文电子版(10页).pdf