Spring实现初始化和销毁bean之前进行的操作,三种方式
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是:通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
1:定义相关的实现类:
- package com.myapp.core.annotation.init;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- public class PersonService {
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- @PostConstruct
- public void init(){
- System.out.println("I'm init method using @PostConstrut...."+message);
- }
- @PreDestroy
- public void dostory(){
- System.out.println("I'm destory method using @PreDestroy....."+message);
- }
- }
2:定义相关的配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <context:annotation-config />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
- package com.myapp.core.annotation.init;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
- PersonService personService = (PersonService)context.getBean("personService");
- personService.dostory();
- }
- }
测试结果:
I'm init method using @PostConstrut....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: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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <!-- <context:annotation-config /> -->
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
同样可以得到以上测试的输出结果。
Spring实现初始化和销毁bean之前进行的操作,三种方式的更多相关文章
- Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 三种不同实现初始化和销毁bean之前进行的操作的比较
Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...
- @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)
前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式
1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...
- spring学习(03)之bean实例化的三种方式
bean实体例化的三种方式 在spring中有三中实例化bean的方式: 一.使用构造器实例化:(通常使用的一个方法,重点) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化 第一种.使用构 ...
- spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...
- Spring学习笔记--初始化和销毁Bean
可以使用bean的init-method和destroy-method属性来初始化和销毁bean.定义一个Hero类: package com.moonlit.myspring; public cla ...
随机推荐
- 烂泥:openvpn配置文件详解
本文由秀依林枫提供友情赞助,首发于烂泥行天下 在上一篇文章<烂泥:ubuntu 14.04搭建OpenVPN服务器>中,我们主要讲解了openvpn的搭建与使用,这篇文章我们来详细介绍下有 ...
- 我也来谈一谈c++模板(一)
c++中程序员使用模板能够写出与类型无关的代码,提高源代码重用,使用合适,大大提高了开发效率.此前,可以使用宏实现模板的功能,但是模板更加安全.清晰.在编写模板相关的代码是我们用到两个关键词:temp ...
- FineReport中如何进行Informix数据库连接
报表开发工具Finereport中,对于Informix数据库,定义数据连接处进行如下配置: 数据库:Others 驱动器:com.informix.jdbc.IfxDriver URL: jdbc: ...
- [转]ASP.NET Core 之 Identity 入门(二)
本文转自:http://www.cnblogs.com/savorboard/p/aspnetcore-identity2.html 前言 在 上篇文章 中讲了关于 Identity 需要了解的单词以 ...
- 更改计算机名称后 导致 sql server 2008 R2 用windows账户不能附加的错误解决办法
出错背景:本人想用sql server 2008 R2 附加sql server 2005数据库出现拒绝访问,后查询网站得知,要用windows身份验证方式登录进去附加, 然后我就用windows身份 ...
- [tem]线段树(白书版)
个人感觉有点坑 add用的标记永久化 set用的标记下传 #include <iostream> #include <cstdio> #include <algorith ...
- git教程链接
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
- 使用Cordova和JQM在ios上需要注意的问题
1.ios编译 cordova platform add ios --save cordova build ios 2.IOS 微信和地图調用問題:因IOS 9.0以上版本白名單限制,衹有加入白名單的 ...
- 聊聊 Web 项目二维码生成的最佳姿势
在设计和实现的过程之后,你永远不知道部署上去的程序会已什么样的姿势运行. 本篇借一次生成二维码逻辑的不同实现,阐述 Web 项目中二维码生成的正确姿势. 文中如有批量,欢迎各位看客老爷拍砖.试运行前5 ...
- 万能的林萧说:我来告诉你,一个草根程序员如何进入BAT。
引言 首先声明,不要再问LZ谁是林萧,林萧就是某著名程序员小说的主角名字. 写这篇文章的目的其实很简单,算是对之前LZ一篇文章的补充和完善. 之前LZ写过一篇<回答阿里社招面试如何准备,顺便谈谈 ...