DN学院讲师招募     Markdown编辑器轻松写博文     TOP 50 CTO坐镇直招     读文章说感想获好礼

通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

分类: Spring 2013-03-16 16:48 24901人阅读 评论(1) 收藏 举报

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和  destory-method方法

第三种是: 通过bean实现InitializingBean和 DisposableBean接口

下面演示通过  @PostConstruct 和 @PreDestory

1:定义相关的实现类:

[java] view plaincopy
  1. package com.myapp.core.annotation.init;
  2. import javax.annotation.PostConstruct;
  3. import javax.annotation.PreDestroy;
  4. public class PersonService {
  5. private String  message;
  6. public String getMessage() {
  7. return message;
  8. }
  9. public void setMessage(String message) {
  10. this.message = message;
  11. }
  12. @PostConstruct
  13. public void  init(){
  14. System.out.println("I'm  init  method  using  @PostConstrut...."+message);
  15. }
  16. @PreDestroy
  17. public void  dostory(){
  18. System.out.println("I'm  destory method  using  @PreDestroy....."+message);
  19. }
  20. }

2:定义相关的配置文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  9. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->
  10. <context:annotation-config />
  11. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
  12. <property name="message" value="123"></property>
  13. </bean>
  14. </beans>

其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;

测试类:

[java] view plaincopy
  1. package com.myapp.core.annotation.init;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest {
  5. public static void main(String[] args) {
  6. ApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");
  7. PersonService   personService  =  (PersonService)context.getBean("personService");
  8. personService.dostory();
  9. }
  10. }

测试结果:

I'm  init  method  using  @PostConstrut....123
I'm  destory method  using  @PreDestroy.....123

其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

类来告诉Spring容器采用的 常用 注解配置的方式:

只需要修改配置文件为:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  9. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->
  10. <!-- <context:annotation-config /> -->
  11. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  12. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
  13. <property name="message" value="123"></property>
  14. </bean>
  15. </beans>

同样可以得到以上测试的输出结果。

转载于:https://www.cnblogs.com/huboking/p/4403497.html

spring init相关推荐

  1. Spring Init Destory

    <beanid="initDestroy"class="com.sanmao.spring.ioc.InitDestroy"init-method=&qu ...

  2. 聊聊Spring Boot服务监控,健康检查,线程信息,JVM堆信息,指标收集,运行情况监控等!...

    来自:https://juejin.im/post/5e2179def265da3e152d2561 前言 去年我们项目做了微服务1.0的架构转型,但是服务监控这块却没有跟上.这不,最近我就被分配了要 ...

  3. Spring Boot 服务监控,健康检查,线程信息,JVM堆信息,指标收集,运行情况监控...

    作者:Richard_Yi 来源:http://39sd.cn/B2A0B 去年我们项目做了微服务1.0的架构转型,但是服务监控这块却没有跟上.这不,最近我就被分配了要将我们核心的微服务应用全部监控起 ...

  4. springboot原生mysql写法_【Rainbond最佳实践】Spring Boot框架配置MySQL

    Rainbond开源软件介绍: Rainbond是国内首个开源的生产级无服务器PaaS. 深度整合基于Kubernetes的容器管理.多类型CI/CD应用构建与交付.多数据中心的资源管理等技术,提供云 ...

  5. SpringBoot快速开发利器:Spring Boot CLI

    今日推荐 推荐一个 Java 接口快速开发框架干掉Random:这个类已经成为获取随机数的王者Docker + Intellij IDEA,提升 10 倍生产力!笑出腹肌的注释,都是被代码耽误的诗人! ...

  6. 快速创建springboot项目:使用http://start.spring.io/网页创建,使用IDEA的Spring Initializr创建,使用SpringBoot CLI创建

    1 快速创建SpringBoot项目 快速创建SpringBoot项目至少可以通过两种方式: 1.使用http://start.spring.io/网页版的创建 2.使用IDEA创建 1.1.通过访问 ...

  7. Spring Boot CMI 使用笔记

    Spring Boot 提供的优越性 1. 自动配置,不需要配置很多的bean, 会根据class 中的jar 文件来自动配置bean 2. 起步依赖,利用Sping starter强大的功能,只需简 ...

  8. spring boot简介_Spring Boot简介

    spring boot简介 在本教程中,我们将看一下Spring Boot,看看它与Spring框架有何不同. 我们还将讨论Spring Boot提供的各种功能. 什么是Spring Boot? 在开 ...

  9. yeoman_具有Spring Boot和Yeoman的单页Angularjs应用程序

    yeoman 我非常感谢yeoman之类的工具,这些工具提供了一种非常快速的方法来将不同的javascript库组合在一起成为一个一致的应用程序. Yeoman提供了UI层,如果您需要开发服务层和静态 ...

  10. 正义联盟的Spring靴

    正义联盟的黑暗时代已经来临,强大的Darkseid即将征服人类. 蝙蝠侠在<神力女超人>的帮助下,努力使联盟与一个关键方面失联. 适当的正义联盟成员管理系统. 由于时间不在他们身边,他们不 ...

最新文章

  1. 设计基于MAX1240,MAX5353的ADDC模块STC8G1KSOP8
  2. 多线程python 客户端fuwuq实现方式_python实现二叉树数据结构的多种遍历方式
  3. linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结
  4. 学点数学(3)-函数空间
  5. warning C4995: “....”: 名称被标记为 #pragma deprecated
  6. linux 清空session,webwork 之销毁session
  7. 闲人闲谈PS之九——项目定义及WBS结构设计
  8. ubuntu 安装迅雷
  9. 如果将编程语言变成女孩子,你更“喜欢”哪些呢?
  10. Python项目:学生管理系统(数据库)
  11. ICDsoft主机半价优惠码推荐
  12. C语言自学之路:养成写博客的习惯,记录自学之路
  13. Quartus II建立新工程流程,Quartus如何建立工程?
  14. linux学习笔记二
  15. 最新校园跑腿微信小程序源码+修复BUG无错
  16. 网络管理技术的亮点以及下一步发展趋势
  17. 云计算变革时代的坚守,九州云“以不变应万变”
  18. 文件上传漏洞(一句话木马)-学习笔记
  19. 有趣的游戏编程学习网站
  20. PHP爬取微信公众号文章(可做为扩展类直接使用)

热门文章

  1. 运维是否有前(钱)途?
  2. 喜提 300w 硬核资产!
  3. linux中级之netfilter防火墙(firewalld)
  4. C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换
  5. JavaScript中对象的属性:如何遍历属性
  6. 安卓深度探索(卷一)第六章
  7. 了解typename的双重意义
  8. JavaScript网页特效---对联广告,网站对联广告
  9. DataGrid的动态绑定问题(二)
  10. 洛谷 P2117 小Z的矩阵