Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
1:定义相应类实现InitializingBean ,DisposableBean 接口
- package com.myapp.core.annotation.init;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- public class PersonService implements InitializingBean,DisposableBean{
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- @Override
- public void destroy() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("I'm init method using implements InitializingBean interface...."+message);
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("I'm init method using implements DisposableBean interface...."+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 class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- -->
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
3:测试类:
- package com.myapp.core.annotation.init;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- AbstractApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
- PersonService personService = (PersonService)context.getBean("personService");
- context.registerShutdownHook();
- }
- }
4:输出测试结果:
- I'm init method using implements DisposableBean interface....123
- 三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose
- INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy
- I'm init method using implements InitializingBean interface....123
over
Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作相关推荐
- 手写简版spring --7--初始化方法和销毁方法
一.目标 当我们的类创建的 Bean 对象,交给 Spring 容器管理以后,这个类对象就可以被赋予更多的使用能力.就像我们在上一章节已经给类对象添加了修改注册Bean定义未实例化前的属性信息修改和实 ...
- Spring Bean的生命周期及接口回调
本篇介绍Spring框架为Spring Bean生命周期各阶段提供的回调接口,程序通过实现回调接口,可以在IOC容器实例化或销毁Bean的过程中,得到Bean的控制权,并对Bean进行预处理工作.通过 ...
- Spring点滴二:Spring Bean
Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...
- Spring Bean 定义
转载自 Spring Bean 定义 Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring I ...
- Spring bean 初始化顺序
InitializingBean, init-method 和 PostConstruct 1.概述 从接口的名字上不难发现,InitializingBean 的作用就是在 bean 初始化后执行定制 ...
- Spring Bean生命周期,就像人的一生
这篇我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 1 简单说说IoC和Bean IoC,控制反转,想必大家都知道,所谓的控制反转,就是把new对象的权利交给容器,所有的对 ...
- Spring Bean、XML方式Bean配置、Bean实例化配置、Bean注入
文章目录 Bean管理 一.SpringBoot Bean 初了解 1.1 了解 1.2 Bean的作用域 1.2.1 注意事项 1.3 第三方Bean 二. 基于XML方式Bean的配置 2.1 S ...
- spring @bean注解
1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...
- Spring Bean的生命周期例子
以下例子源于:W3Cschool,在此作记录 HelloWorld.java package com.how2java.w3cschool.beanlife;public class HelloWor ...
- 11 张流程图帮你搞定 Spring Bean 生命周期 (核心)
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:腾讯推出高性能 RPC 开发框架 个人原创100W+访问量博客:点击前往,查看更多 来源:www.jianshu ...
最新文章
- VS combox控件的简单用法
- python随机画散点图-python散点图实例之随机漫步
- 纯C写Windows程序系列---------VS2010设置纯C环境 .
- python网络编程自学_五分钟搞定Python网络编程实现TCP和UDP连接
- 创建mysql视图语法正确的是_MySQL创建视图的语法格式
- webstorm代码行数统计_来测试下 2019 你一共写了多少行代码?
- C语言小知识---printf()函数
- 【技术分享】几维安全CTO刘柏江:IoT时代LLVM编译器防护的艺术
- linux自动截屏脚本,Android实现自动截图脚本
- 贾老板大秀未来机器人之舞,场面有点不可描述(动图+视频)
- linux input设备冲突,linux input设备怎么固定event handler
- 追溯计算机的本源,读电路与系统简史
- 国密SM2非对称加密算法(对本地文件的加解密)代码展示
- mysql 钩子_SetWindowsHookEx原理(如何使用钩子,使用钩子hook其他进程的函数)
- 英语不好,也能让你看懂man手册
- Excel-单纯形法(大M法)求解 直接求解与规划求解功能
- 记事本html表白,霸道总裁之代码强势表白
- paip.手机电话本备份导入到pc管理attilax总结
- 《SAP后勤模块实施攻略—SAP在生产、采购、销售、物流中的应用》——第2章 生产模式和计划策略 2.1 计划策略与需求类型总览...
- 大白话理解可达性分析算法