`
heipark
  • 浏览: 2102027 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(转)Spring anotation实现xml中init方法功能

    博客分类:
  • Java
 
阅读更多

转自:http://www.mkyong.com/spring/spring-postconstruct-and-predestroy-example/

原文:Spring @PostConstruct And @PreDestroy Example

In Spring, you can either implements InitializingBean and DisposableBean interface or specify the init-method and destroy-method in bean configuration file for the initialization and destruction callback function. In this article, we show you how to use annotation @PostConstruct and @PreDestroy to do the same thing.

Note
The @PostConstruct and @PreDestroy annotation are not belong to Spring, it’s located in the J2ee library – common-annotations.jar.

@PostConstruct and @PreDestroy

A CustomerService bean with @PostConstruct and @PreDestroy annotation

package com.mkyong.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");
	}
 
}

By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘<context:annotation-config />‘ in bean configuration file,

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.mkyong.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.mkyong.customer.services.CustomerService">
		<property name="message" value="i'm property message" />
	</bean>
 
</beans>

Run it

package com.mkyong.common;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.mkyong.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();
    }
}

Output

Init method after properties are set : im property message
com.mkyong.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

The initIt() method (@PostConstruct) is called, after the message property is set, and the cleanUp() method (@PreDestroy) is call after the context.close();()

分享到:
评论

相关推荐

    spring的练习小sample

    3. **spring_1100_AOP_xml**:这部分可能展示了如何使用XML配置实现Spring的AOP功能。AOP允许我们在不修改源代码的情况下,对应用程序的特定部分(切点)进行拦截,添加额外的行为(通知),如日志记录、性能监控等...

    大家看看我设计的泛型DAO(使用Spring的Anotation和Hibernate)

    `UserDaoImpl`则实现这些方法,通过调用`HibernateBaseDao`中的通用方法来完成实际的数据库操作。 在`我设计的泛型DAO.jpg`中,可能展示了这个设计的类图或者代码结构,进一步解释了各个组件如何协作以实现泛型DAO...

    spring-core-anotation

    在Spring框架中,注解和Java配置是两种主要的组件声明方式,它们使得开发者无需编写XML配置文件,就能实现依赖注入和其他功能。本文将深入探讨Spring核心注解以及Java配置的相关知识。 ### Spring核心注解 1. `@...

    spring_hibernate_anotations

    在现代Java开发中,Spring和Hibernate是两个非常关键的工具,Spring作为一个全面的轻量级应用框架,提供了依赖注入、面向切面编程等功能,而Hibernate则是一个强大的对象关系映射(ORM)框架,简化了数据库操作。...

    java Anotation

    ### Java Annotation详解 #### 一、什么是Java Annotation?...例如,Spring框架广泛使用注解来实现依赖注入和其他功能。了解并掌握Java注解的基本概念和使用方法,有助于提高代码质量和开发效率。

    利用java反射、注解及泛型模拟ORM实现

    在Java编程中,反射、注解(Annotation)和泛型是三个非常重要的特性,它们各自...在实际项目中,成熟的ORM框架如Hibernate、MyBatis等已经实现了这些功能,但理解其工作原理对于优化和定制自己的ORM解决方案至关重要。

    hibernate anotation 处理一对多关系

    正确的配置可以帮助开发者有效地实现数据库表结构与Java对象模型之间的映射,以及对关系数据库进行操作时的导航。 此外,Hibernate还支持使用XML配置文件来指定映射,但注解方式因其代码级的简洁性和易于维护的特性...

    hibernate_reference_anotation 3.5.0-final pdf

    - **配置**:在 persistence.xml 文件中启用 Bean Validation 功能。 - **捕获验证异常**:通过异常处理机制捕获并处理验证失败情况。 ##### 6.2 Hibernate Validator - **描述**:Hibernate Validator 是一款实现...

    Annotation的关联关系

    在Java编程语言中,注解(Annotation)是一种元数据,它提供了一种安全的方法来将信息附加到代码中,而不直接影响代码的运行。注解在软件开发中扮演着重要的角色,用于编译时检查、运行时处理、文档生成等场景。这篇...

    词法分析器-计算器-设计与实现实验报告..pdf

    在该实验中,我们也学习到了词法分析器的原理和实现方法,并且掌握了C语言的基本语法规则。这些知识将对我们的编程和软件开发有重要的影响。 最后,该实验报告也对我们提供了一个很好的机会来实践我们的编程和软件...

    anotation_tool

    "anotation_tool"是一款专为机器学习(ML)团队设计的注释工具,它通过极端单击的方式使得创建边界框变得更加便捷。这个工具的核心目的是帮助ML团队在图像数据集上进行标注工作,以便训练和优化计算机视觉模型。下面...

    hibernate-annotation

    在实际项目中,结合Spring Boot和Hibernate注解,可以快速构建出高效的数据访问层。通过注解配置,可以轻松实现CRUD操作,并进行复杂的查询。 四、最佳实践与注意事项 1. 合理使用注解:不是所有字段都需要注解,...

    SSH框架搭建

    SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。这个框架集合提供了模型-视图-控制器(MVC)架构模式,以及依赖注入(DI)和面向切面编程(AOP)的能力,大大简化了Web...

    2个案例 自定义annotation进行查询对象封装 itext 打印案例

    在IT行业中,自定义注解(Annotation)是Java编程语言中的一个重要特性,它允许程序员在代码中嵌入元数据,增强了代码的可读性和可维护性。这些元数据可以被编译器或运行时环境用来执行特定的任务,例如代码分析、...

    Struts2 json插件入门指南.pdf

    - 在`struts.xml`中,可以使用`root`属性指定要返回的JSON对象的根节点名称: ```xml &lt;param name="root"&gt;user ``` - 这样返回的JSON数据会包含一个名为`user`的对象作为根节点。 #### 5. 示例3:...

    java-annotation-processor:JAVA注释处理器

    在Java世界中,注解处理器是通过实现`javax.annotation.processing.Processor`接口来创建的。处理器通常用于验证注解的使用、生成额外的源文件(如DAO层、Service层的代码自动生成)、或者在编译时进行类型检查等。...

    AdventureWorksDB

    这个数据库包含了各种业务场景的数据,如销售、人力资源、生产等,为学习和演示SQL Server的功能提供了丰富的素材。"temr"可能是“template”或者“temporal”的误拼,暗示AdventureWorksDB可能被用作模板或涉及时间...

    vscode-annotator:Visual Studio代码扩展。 显示git blame信息并提供轻松的提交差异

    在提交差异中,注释差异视图左侧的内容: 从提交差异中,在同一提交中打开另一个文件的差异: 具有不同颜色设置等的注释:请求功能或报告错误非常欢迎功能请求和错误报告: : 当您提出github问题时,有几个我的要求...

    利用Gradle进行Struts 2.5.14.1 注解式开发项目

    (1)struts2-anotation.war 打包发布的war可以直接发布到tomcat服务器 (2)struts2-anotation-lib.zip 注解式开发需要用的lib包 (3)struts2-anotation_eclipse.zip 注解时开发myeclipse项目压缩 (4)struts2-...

    BPMN2.0-概要

    BPMN(BusinessProcessModelAndNotation)- 业务流程模型和符号是有BPMI(BusinessProcessManagementInitiative)开发的一套变准的业务流程建模符号。2004年5月发布了BPMN1.0规范.BPMI于2005年9月并入OMG(The ...

Global site tag (gtag.js) - Google Analytics