Spring Bean init-method 和 destroy-method实例
示例
package com.yiibai.customer.services;public class CustomerService
{String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public void initIt() throws Exception {System.out.println("Init method after properties are set : " + message);}public void cleanUp() throws Exception {System.out.println("Spring Container is destroy! Customer clean up");}}
File : applicationContext.xml, 在bean中定义了init-method和destroy-method属性。
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.yiibai.customer.services.CustomerService" init-method="initIt" destroy-method="cleanUp"><property name="message" value="i'm property message" /></bean></beans>
执行下面的程序代码:
package com.yiibai.common;import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.customer.services.CustomerService;public class App
{public static void main( String[] args ){ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});CustomerService cust = (CustomerService)context.getBean("customerService");System.out.println(cust);context.close();}
}
输出
Init method after properties are set : I'm property message
com.yiibai.customer.services.CustomerService@5f49d886
Spring Container is destroy! Customer clean up
Spring Bean init-method 和 destroy-method实例相关推荐
- spring bean生命周期管理--转
Life Cycle Management of a Spring Bean 原文地址:http://javabeat.net/life-cycle-management-of-a-spring-be ...
- spring bean的创建,生命周期
1. 初探spring 什么是spring Spirng 是分层的Java se/ee应用full-stack(web层mvc dao层jdbc模板 业务事务管理)轻量级开源框架,以IoC(inver ...
- (Do not request a bean from a BeanFactory in a destroy method implementation!)
异常:Singleton bean creation not allowed while singletons of this factory are in destruction (Do not r ...
- Error creating bean with name ‘multipartResolver‘: Lookup method resolution failed; 上传文件异常
浏览器错误信息: 错误信息: HTTP状态 500 - 内部服务器错误 类型 异常报告消息 Servlet[springmvctest]的Servlet.init()引发异常描述 服务器遇到一个意外的 ...
- spring bean的init、destory的几种方法及生命周期
个人总结spring-bean的声明周期维护有三种: 注解:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 xml中配置:xml中定义ini ...
- Spring Bean默认配置为单实例 Spring Bean生命周期
2019独角兽企业重金招聘Python工程师标准>>> Spring 的Bean默认的是单例的. 如果不想单例需要如下配置: <bean id="user" ...
- Spring Bean配置默认为单实例 pring Bean生命周期
Bean默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." scope="singleton& ...
- spring 笔记2:Spring MVC : Did not find handler method for 问题的解决
spring 笔记2:Spring MVC : Did not find handler method for 问题的解决 参考文章: (1)spring 笔记2:Spring MVC : Did n ...
- Spring bean的生命周期init和destory
Bean生命周期:定义 --- 初始化 --- 使用 --- 销毁 一.初始化: 方法1.实现org.springframework.beans.foctory.InitializingBean接口, ...
- react项目在ie11浏览器运行报错Instance method `debug.destroy()` is deprecated and no longer does anything
react项目运行时,ie11浏览器报错:Instance method `debug.destroy()` is deprecated and no longer does anything. It ...
最新文章
- linux中cp:overwrite提示的问题
- JAVA并发编程8_线程池的使用
- JSON Funcs
- js工作笔记001---javascript中(function($){...})(jQuery)写法是什么意思
- Mac使用ssh公钥免密登录服务器
- android内核源码下载和编译
- 系统简单的UIImagePickerController
- 空间计量模型_5种经典空间计量模型的回归命令、程序及原始数据:SAR模型、SDM模型、SAC模型、SEM模型及GSPRE模型...
- windows cmd 快速编辑模式
- web逻辑思维题目_Java Web面试题整理(思维导图)
- 使用VM Ware创建虚拟机
- python 爬取 强智科技教务系统(湖南)
- 测试网页版淘宝购物车
- Ultra Recal 一款DIY的个人管理软件
- 最后一公里极速配送(一)
- 高德地图上线新能源导航 一站式充电服务缓解里程焦虑
- PhotoshopCC2019(64位)下载+安装教程
- 【7.talker.py listener.py】转载自他人作品,相关专业知识咨询相关人员!
- 使用计算机正确坐姿,如何保持正确坐姿?(多图)
- 数商云跨境电商平台运营总结:整合渠道+深引流量