- 浏览: 2102027 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (366)
- hadoop (91)
- DB (2)
- vmware (0)
- python (49)
- Java (72)
- Linux (57)
- 多线程 (2)
- hive (1)
- hbase (3)
- mongodb (5)
- Hibernate (3)
- nodejs (1)
- 模式与重构 (1)
- 持续集成CI (4)
- Mysql (2)
- chorme (1)
- 软件开发 (0)
- 敏捷 (5)
- maven (3)
- windows (7)
- 测试驱动 (1)
- scala (3)
- Jetty (0)
- android (1)
- eclipse (1)
- 设计模式 (1)
- 面向对象设计 (2)
- oracle (3)
- cassandra (15)
- pig (3)
- rails (1)
- redis (4)
- ruby (1)
- storm (0)
- 实时运算 (3)
- scribe (1)
- hadoop源码 (3)
- cassandra jna (1)
- Kafka (2)
- Sublime (2)
- C++ (2)
- C (1)
- 版本控制 (1)
- 服务器 (1)
- docker (0)
- flink (1)
最新评论
-
伍大都督:
解释太到位了,感谢分享
理解Linux系统中的load average(图文版) -
rfjian123:
非常感谢,用你的方法解决了问题。
Server sent unexpected return value (403 Forbidden) in response to OPTIONS -
yuhaifei12:
今天遇到同样的问题了。设置的是每分钟执行一次。结果发现每分钟执 ...
解决Linux下crontab同一时间重复执行问题 -
BigBird2012:
想问一下,使用ExecutorService每次都要调用 sh ...
spring quartz使用多线程并发“陷阱” -
zhuqx1130:
有用,谢谢
解决Sublime Text 3中文显示乱码(tab中文方块)问题
转自: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.
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();()
发表评论
-
shell下使用log4j 1.x “No appenders could be found for logger”问题两个处理办法
2018-05-25 23:25 1666错误: log4j:WARN No appenders c ... -
通过java class文件确定其编译器版本
2016-06-23 10:45 1089方法一: hexdump -C XXX.class ... -
字符编码笔记:ASCII,Unicode和UTF-8(转)
2014-11-06 17:29 1250今天中午,我突然想搞 ... -
Server sent unexpected return value (403 Forbidden) in response to OPTIONS
2014-10-10 15:01 5575之前用的好好的,忽然某天再次svn up时候就报下面的错误: ... -
netbeans下优秀sublinme主题
2014-09-29 10:13 3652推荐站点:http://netbeansthemes.com ... -
将netbeans项目放到jenkins上做持续集成
2014-07-25 15:20 1247netbeans项目本质是通过ANT来管理的,只不 ... -
关于jvm中Xmx参数默认值
2014-05-08 09:19 2034我的机器win7 64bit 8GB内存,通过jconso ... -
Eclipse代码自动完成功能无法使用问题记录
2014-04-28 10:15 1169使用Alt+/无法补齐Java代码,现象时没有反应。解决方 ... -
thrift0.9.1简单教程(包含Java服务端和Java、python客户端)
2014-04-25 15:55 6443一、Thrift Java服务端和客户端 官 ... -
使用sublime text3开发scala
2014-04-04 16:49 3817打开菜单栏Tool->build system-> ... -
netbeans下打开本地文件夹插件(explorer)
2014-04-02 14:44 20451. 在线安装 工具->插件->可用插 ... -
关闭Sublime自动更新
2014-03-09 21:42 874每次启动都提示更新,可以关闭它。 找到Prefere ... -
使用log4j.properties配置slf4j输出LOG
2014-01-09 08:57 6709完成配置需要以下文件: slf4j-log4j12-x ... -
正确使用java -cp通配符
2013-11-21 17:05 10222JDK6支持java -cp后面跟通配符'*',试了一下发 ... -
强制Java使用东八时区方法
2013-11-15 15:37 1859今天线上有台服务器时区错误,导致很多使用new Da ... -
谨慎使用java的PrintWriter类
2013-10-23 12:31 1381public void test() throws Fil ... -
记录Java ShutdownHook
2013-08-30 11:40 1084public class TestMe { stat ... -
eclipse + maven + jetty + spring web 开发环境简要笔记
2013-08-27 11:25 1624环境准备 确保安装maven3 确保安装eclip ... -
Eclipse(IDE for Java Developers)、maven、jetty、spring web集成
2013-08-27 08:53 0方法一: http://wiki.eclipse.o ... -
关于数据压缩
2013-08-23 14:26 1146常用压缩 quicklz zlib snappy/ ...
相关推荐
3. **spring_1100_AOP_xml**:这部分可能展示了如何使用XML配置实现Spring的AOP功能。AOP允许我们在不修改源代码的情况下,对应用程序的特定部分(切点)进行拦截,添加额外的行为(通知),如日志记录、性能监控等...
`UserDaoImpl`则实现这些方法,通过调用`HibernateBaseDao`中的通用方法来完成实际的数据库操作。 在`我设计的泛型DAO.jpg`中,可能展示了这个设计的类图或者代码结构,进一步解释了各个组件如何协作以实现泛型DAO...
在Spring框架中,注解和Java配置是两种主要的组件声明方式,它们使得开发者无需编写XML配置文件,就能实现依赖注入和其他功能。本文将深入探讨Spring核心注解以及Java配置的相关知识。 ### Spring核心注解 1. `@...
在现代Java开发中,Spring和Hibernate是两个非常关键的工具,Spring作为一个全面的轻量级应用框架,提供了依赖注入、面向切面编程等功能,而Hibernate则是一个强大的对象关系映射(ORM)框架,简化了数据库操作。...
### Java Annotation详解 #### 一、什么是Java Annotation?...例如,Spring框架广泛使用注解来实现依赖注入和其他功能。了解并掌握Java注解的基本概念和使用方法,有助于提高代码质量和开发效率。
在Java编程中,反射、注解(Annotation)和泛型是三个非常重要的特性,它们各自...在实际项目中,成熟的ORM框架如Hibernate、MyBatis等已经实现了这些功能,但理解其工作原理对于优化和定制自己的ORM解决方案至关重要。
正确的配置可以帮助开发者有效地实现数据库表结构与Java对象模型之间的映射,以及对关系数据库进行操作时的导航。 此外,Hibernate还支持使用XML配置文件来指定映射,但注解方式因其代码级的简洁性和易于维护的特性...
- **配置**:在 persistence.xml 文件中启用 Bean Validation 功能。 - **捕获验证异常**:通过异常处理机制捕获并处理验证失败情况。 ##### 6.2 Hibernate Validator - **描述**:Hibernate Validator 是一款实现...
在Java编程语言中,注解(Annotation)是一种元数据,它提供了一种安全的方法来将信息附加到代码中,而不直接影响代码的运行。注解在软件开发中扮演着重要的角色,用于编译时检查、运行时处理、文档生成等场景。这篇...
在该实验中,我们也学习到了词法分析器的原理和实现方法,并且掌握了C语言的基本语法规则。这些知识将对我们的编程和软件开发有重要的影响。 最后,该实验报告也对我们提供了一个很好的机会来实践我们的编程和软件...
"anotation_tool"是一款专为机器学习(ML)团队设计的注释工具,它通过极端单击的方式使得创建边界框变得更加便捷。这个工具的核心目的是帮助ML团队在图像数据集上进行标注工作,以便训练和优化计算机视觉模型。下面...
在实际项目中,结合Spring Boot和Hibernate注解,可以快速构建出高效的数据访问层。通过注解配置,可以轻松实现CRUD操作,并进行复杂的查询。 四、最佳实践与注意事项 1. 合理使用注解:不是所有字段都需要注解,...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架集成。这个框架集合提供了模型-视图-控制器(MVC)架构模式,以及依赖注入(DI)和面向切面编程(AOP)的能力,大大简化了Web...
在IT行业中,自定义注解(Annotation)是Java编程语言中的一个重要特性,它允许程序员在代码中嵌入元数据,增强了代码的可读性和可维护性。这些元数据可以被编译器或运行时环境用来执行特定的任务,例如代码分析、...
- 在`struts.xml`中,可以使用`root`属性指定要返回的JSON对象的根节点名称: ```xml <param name="root">user ``` - 这样返回的JSON数据会包含一个名为`user`的对象作为根节点。 #### 5. 示例3:...
在Java世界中,注解处理器是通过实现`javax.annotation.processing.Processor`接口来创建的。处理器通常用于验证注解的使用、生成额外的源文件(如DAO层、Service层的代码自动生成)、或者在编译时进行类型检查等。...
这个数据库包含了各种业务场景的数据,如销售、人力资源、生产等,为学习和演示SQL Server的功能提供了丰富的素材。"temr"可能是“template”或者“temporal”的误拼,暗示AdventureWorksDB可能被用作模板或涉及时间...
在提交差异中,注释差异视图左侧的内容: 从提交差异中,在同一提交中打开另一个文件的差异: 具有不同颜色设置等的注释:请求功能或报告错误非常欢迎功能请求和错误报告: : 当您提出github问题时,有几个我的要求...
(1)struts2-anotation.war 打包发布的war可以直接发布到tomcat服务器 (2)struts2-anotation-lib.zip 注解式开发需要用的lib包 (3)struts2-anotation_eclipse.zip 注解时开发myeclipse项目压缩 (4)struts2-...
BPMN(BusinessProcessModelAndNotation)- 业务流程模型和符号是有BPMI(BusinessProcessManagementInitiative)开发的一套变准的业务流程建模符号。2004年5月发布了BPMN1.0规范.BPMI于2005年9月并入OMG(The ...