关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种,通过在xml中定义init-method和destory-method方法
第二种,通过bean实现InitializingBean和 DisposableBean接口
第三种,通过Spring @PostConstruct和@PreDestroy方法
一.通过在xml中定义init-method和destory-method方法
在xml中配置init-method和 destory-method方法,只是定义spring容器在初始化bean和容器销毁之前的所做的操作。
1.xml中配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <bean id="testService" class="com.bijian.study.service.impl.TestService" init-method="afterPropertiesSet" destroy-method="destroy"> <property name="message" value="123"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> </beans>
2.定义TestService类
package com.bijian.study.service.impl; public class TestService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void afterPropertiesSet() throws Exception { System.out.println("I'm init method using @PostConstrut...."+message); } public void destroy() throws Exception { System.out.println("I'm destory method using @PreDestroy....."+message); } }
3.相应的测试类
package com.bijian.study.test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bijian.study.service.impl.TestService; public class MainTest { public static void main(String[] args) { try { AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:/develop/eclipse/workspace/SpringMVC/WebContent/WEB-INF/springMVC-servlet.xml"); TestService testService = (TestService)context.getBean("testService"); System.out.println(testService.getMessage()); //testService.destroy(); context.registerShutdownHook(); } catch (Exception e) { e.printStackTrace(); } } }
运行结果:
I'm init method using @PostConstrut....123 123 I'm destory method using @PreDestroy.....123
可以看出init-method方法和destroy-method方法都已经执行了。
context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。
二.通过bean实现InitializingBean和 DisposableBean接口
1.xml中配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <bean id="testService" class="com.bijian.study.service.impl.TestService"> <property name="message" value="123"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> </beans>
2.定义TestService类
package com.bijian.study.service.impl; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class TestService implements InitializingBean,DisposableBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void afterPropertiesSet() throws Exception { System.out.println("I'm init method using @PostConstrut...."+message); } public void destroy() throws Exception { System.out.println("I'm destory method using @PreDestroy....."+message); } }
3.测试类同上,运行测试类。
运行结果:
I'm init method using @PostConstrut....123 123 I'm destory method using @PreDestroy.....123
三.通过Spring @PostConstruct和@PreDestroy方法
1.xml中配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <context:component-scan base-package="com.bijian.study"></context:component-scan> <bean id="testService" class="com.bijian.study.service.impl.TestService"> <property name="message" value="123"></property> </bean> <context:annotation-config /> <mvc:annotation-driven></mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> </beans>
其中<context:annotation-config />告诉spring 容器采用注解配置,扫描注解配置。
2.定义TestService类
package com.bijian.study.service.impl; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class TestService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @PostConstruct public void afterPropertiesSet() throws Exception { System.out.println("I'm init method using @PostConstrut...."+message); } @PreDestroy public void destroy() throws Exception { System.out.println("I'm destory method using @PreDestroy....."+message); } }
3.测试类同上,运行测试类。
运行结果:
I'm init method using @PostConstrut....123 123 I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor类来告诉Spring容器采用的 常用解配置的方式,只需要修改配置文件为:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <mvc:annotation-driven></mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="testService" class="com.bijian.study.service.impl.TestService"> <property name="message" value="123"></property> </bean> </beans>
最后,Srping的bean可以通过注解配置方法注入。如下所示:
1.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <context:component-scan base-package="com.bijian.study"></context:component-scan> <bean id="testService" class="com.bijian.study.service.impl.TestService"> <property name="message" value="123"></property> </bean> <context:annotation-config /> <mvc:annotation-driven></mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> </beans>
2.定义TestService类
package com.bijian.study.service.impl; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.stereotype.Service; @Service("testService") public class TestService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @PostConstruct public void afterPropertiesSet() throws Exception { System.out.println("I'm init method using @PostConstrut...."+message); } @PreDestroy public void destroy() throws Exception { System.out.println("I'm destory method using @PreDestroy....."+message); } }
参考文章:
相关推荐
主要介绍了Spring Bean的初始化和销毁,结合实例形式详细分析了Spring Bean的初始化和销毁相关配置、使用方法及操作注意事项,需要的朋友可以参考下
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务。 Spring IOC容器对Bean的生命周期...在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法。
主要介绍了Spring Bean初始化及销毁多种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
* IOC:spring容器控制对象的生命周期:前提条件:在spring容器中的bean必须是单例的 * 创建 * 方式 * 利用默认的构造函数,如果没有默认的构造函数,会报错 * 利用静态工厂方法 * 利用实例工厂方法 * 时机 *...
SSH笔记-IOC容器中 Bean 的生命周期,通过实现BeanPostProcessor接口,来定义初始化方法和销毁方法时的逻辑
本文深入探讨了Spring框架中IoC容器的源码机制,涵盖了容器的初始化、Bean工厂的实例化、Bean定义的读取及Spring Bean的生命周期管理。通过精细的分析,本文揭示了AnnotationConfigApplicationContext的实例化过程,...
为了便于这些工作的设计,Spring IoC 容器提供了相关的功能,可以让应用定制 Bean 的初始化和销毁过程。 Bean 生命周期 的 init-method 属性或 @Bean 注解的 initMethod 属性),那么将调用该方法。 如果存在与 ...
作者:钱安川(Moxie) ...Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22
Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22 注:后面的...
> 销毁之前调用的回调方法。 > Spring 框架提供了以下四种方式来管理 bean 的生命周期事件: > InitializingBean 和 DisposableBean 回调接口 > 针对特殊行为的其他 Aware 接口 > Bean 配置文件中的 Custom init()...
Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定...
Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml 1.2 实体类JavaBean 1.2.1 User类 ...1.9.1 实体类JavaBean User加自定义的初始化方法和销毁方法 1.9.3 加了lazy
初始化:<bean init-method=""/> e.资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用...
初始化:<bean init-method=""/> e.资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用...
一般情况下,我们只是关心如何正确地将Bean装配到...但是恰恰有时候,我们需要自定义初始化或销毁Bean的过程,以满足一些“特殊的”需求。比如,数据源在关闭的时候调用其close方法,这是项目开发过程中很常见的需求。
4.4.4 延迟初始化bean . 55 4.4.5 自动装配协作者.. 55 4.4.5.1 自动装配的限制和缺点.. 56 4.4.5.2 从自动装配中排除bean. 57 4.4.6 方法注入. 57 4.4.6.1 查找方法注入.. 58 4.4....
初始化:<bean init-method=""/> e.资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用...