- Spring初学快速入门
- 安装Spring工具套件到Eclipse
- Spring快速入门
- Spring自动装配Bean
- Spring JDBC支持
- Spring JavaConfig
- Spring 依赖注入(DI)
-
Spring Bean基础
- Spring Bean引用例子
- 如何注入值到Spring bean属性
- Spring bean加载多个配置文件
- Spring内部bean实例
- Spring Bean作用域实例
- Spring集合 (List,Set,Map,Properties) 实例
- Spring ListFactoryBean实例
- Spring SetFactoryBean实例
- Spring MapFactoryBean例子
- Spring注入日期到bean属性-CustomDateEditor
- Spring PropertyPlaceholderConfigurer实例
- Spring bean配置继承
- Spring依赖检查
- Spring使用@Required注解依赖检查
- Spring自定义@Required-style注解
- Spring Bean InitializingBean和DisposableBean实例
- Spring Bean init-method 和 destroy-method实例
- Spring @PostConstruct和@PreDestroy实例
- Spring表达式语言
- Spring自动组件扫描
- Spring AOP (面向方面编程)
- Spring AOP + AspectJ框架
- Spring Hibernate支持
- Spring E-mail支持
-
Spring与其它Web框架集成
Spring @PostConstruct和@PreDestroy实例
在Spring中,既可以实现 InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函数。在这篇文章中,我们将介绍如何使用 @PostConstruct 和 @PreDestroy 注解来做同样的事情。
注:@PostConstruct和@PreDestroy 标注不属于 Spring,它是在J2EE库- common-annotations.jar。
@PostConstruct 和 @PreDestroy
一个 CustomerService Bean使用 @PostConstruct 和 @PreDestroy 注释
package com.zyiz.customer.services; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @PostConstruct public void initIt() throws Exception { System.out.println("Init method after properties are set : " + message); } @PreDestroy public void cleanUp() throws Exception { System.out.println("Spring Container is destroy! Customer clean up"); } }
默认情况下,Spring不会意识到@PostConstruct和@PreDestroy注解。要启用它,要么注册“CommonAnnotationBeanPostProcessor”,要么在bean配置文件的<context:annotation-config />‘ 指定,
1. CommonAnnotationBeanPostProcessor
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="customerService" class="com.zyiz.customer.services.CustomerService"> <property name="message" value="i'm property message" /> </bean> </beans>
2. <context:annotation-config />
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="customerService" class="com.zyiz.customer.services.CustomerService"> <property name="message" value="i'm property message" /> </bean> </beans>
执行结果
package com.zyiz.netmon; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyiz.customer.services.CustomerService; public class App { public static void main( String[] args ) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close(); } }
输出结果
Init method after properties are set : im property message com.zyiz.customer.services.CustomerService@47393f ... INFO: Destroying singletons in org.springframework.beans.factory. support.DefaultListableBeanFactory@77158a: defining beans [customerService]; root of factory hierarchy Spring Container is destroy! Customer clean up
initIt()方法(@PostConstruct)被调用时,消息属性设置后 cleanUp() 方法(@PreDestroy)是在context.close()执行后被调用;
下载源代码 – http://pan.baidu.com/s/1qX2W6xI
分类导航
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
关注微信小程序
扫描二维码
程序员编程王