- 浏览: 20788 次
- 性别:
- 来自: 北京
文章分类
最新评论
(本文为自己书写,如有错误,请指正,大家共同进步,谢谢。)
Bean的销毁:
1.destroy-method属性;
<java 代码>
package com.deciphering;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleBean {
private static final String DEFAULT_NAME = "Mark";
private static final String DE_AGE = "20";
//添加setter getter方法
private String age = "0";
private String name;
public String getAge() {
return age;
}
public void setAge(String age) {
System.out.println("spring依赖注入。。");
this.age = age;
System.out.println("age" + this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SimpleBean(){
System.out.println("spring实例化bean。。。。");
}
//清理资源并回收
public void close(){
System.out.println("调用close()...");
}
public String toString(){
return name +"name\n" + age +"age\n";
}
//运行测试main的方法
public static void main(String[]args){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("simpleBean");//获取simpleBean实例
context.registerShutdownHook();//为spring注册关闭钩子
System.out.println("关闭。。。");
}
}
<.xml配置(局部)>
<bean name="simpleBean" class="com.deciphering.SimpleBean" destroy-method="close">
<property name="name" value="Bill"></property>
<property name="age" value="20"></property>
</bean>
<运行结果>
/*spring实例化bean。。。。
*spring依赖注入。。
*age20
*关闭。。。
*调用close()...
*/
2.DisposableBean接口;
package com.deciphering;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleBean implements DisposableBean{
private static final String DEFAULT_NAME = "Mark";
private static final String DE_AGE = "20";
//添加setter getter方法
private String age = "0";
private String name;
public String getAge() {
return age;
}
public void setAge(String age) {
System.out.println("spring依赖注入。。");
this.age = age;
System.out.println("age" + this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SimpleBean(){
System.out.println("spring实例化bean。。。。");
}
public String toString(){
return name +"name\n" + age +"age\n";
}
//运行测试main的方法
public static void main(String[]args){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("simpleBean");//获取simpleBean实例
context.registerShutdownHook();//为spring注册关闭钩子
System.out.println("关闭。。。");
}
public void destroy() throws Exception{
System.out.println("代用close()。。。。");
}
}
<.xml(局部)>
<bean name="simpleBean" class="com.deciphering.SimpleBean" >
<property name="name" value="Bill"></property>
<property name="age" value="20"></property>
</bean>
<运行结果>
/*
* spring实例化bean。。。。
spring依赖注入。。
age20
关闭。。。
代用close()。。。。
*/
Bean的销毁:
1.destroy-method属性;
<java 代码>
package com.deciphering;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleBean {
private static final String DEFAULT_NAME = "Mark";
private static final String DE_AGE = "20";
//添加setter getter方法
private String age = "0";
private String name;
public String getAge() {
return age;
}
public void setAge(String age) {
System.out.println("spring依赖注入。。");
this.age = age;
System.out.println("age" + this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SimpleBean(){
System.out.println("spring实例化bean。。。。");
}
//清理资源并回收
public void close(){
System.out.println("调用close()...");
}
public String toString(){
return name +"name\n" + age +"age\n";
}
//运行测试main的方法
public static void main(String[]args){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("simpleBean");//获取simpleBean实例
context.registerShutdownHook();//为spring注册关闭钩子
System.out.println("关闭。。。");
}
}
<.xml配置(局部)>
<bean name="simpleBean" class="com.deciphering.SimpleBean" destroy-method="close">
<property name="name" value="Bill"></property>
<property name="age" value="20"></property>
</bean>
<运行结果>
/*spring实例化bean。。。。
*spring依赖注入。。
*age20
*关闭。。。
*调用close()...
*/
2.DisposableBean接口;
package com.deciphering;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleBean implements DisposableBean{
private static final String DEFAULT_NAME = "Mark";
private static final String DE_AGE = "20";
//添加setter getter方法
private String age = "0";
private String name;
public String getAge() {
return age;
}
public void setAge(String age) {
System.out.println("spring依赖注入。。");
this.age = age;
System.out.println("age" + this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SimpleBean(){
System.out.println("spring实例化bean。。。。");
}
public String toString(){
return name +"name\n" + age +"age\n";
}
//运行测试main的方法
public static void main(String[]args){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("simpleBean");//获取simpleBean实例
context.registerShutdownHook();//为spring注册关闭钩子
System.out.println("关闭。。。");
}
public void destroy() throws Exception{
System.out.println("代用close()。。。。");
}
}
<.xml(局部)>
<bean name="simpleBean" class="com.deciphering.SimpleBean" >
<property name="name" value="Bill"></property>
<property name="age" value="20"></property>
</bean>
<运行结果>
/*
* spring实例化bean。。。。
spring依赖注入。。
age20
关闭。。。
代用close()。。。。
*/
发表评论
-
jdk tomcat 环境变量配置
2015-07-09 21:32 405Win7 下如何配置java环境变量 1。准备jdk包,wi ... -
页面有两个frame,怎样点击一个的链接改变另一个的内容
2015-04-07 13:58 1416<a src="地址" targe ... -
java代码怎么实现分页?求指教!!
2015-03-31 14:22 42Java实现分页源码及其思想 -
javaScript (1)
2015-03-24 17:19 536JavaScript 1、运算符 运算符就是完成操作的一 ... -
MyEclipse里部分习惯的更改
2015-03-20 17:30 475继续补充中---------------------- ... -
javaSE javaEE javaME == API下载
2015-03-20 14:06 796oracle下载各种API文档: http://www.ora ... -
spring------>>cvc-elt.1: Cannot find the declaration of element
2015-03-18 16:49 488将-------- <?xml version=&qu ... -
log4j
2015-03-18 16:14 356链接: http://www.blogjava.net/ ... -
PowerDesigner使用教程
2015-03-17 17:09 426链接跳转: http://www.cnblogs.com/la ... -
js乱码
2015-03-17 15:43 415检查你js文件的编码格式(简单的办法就是在dreamweave ... -
hibernate三种 状态
2015-03-16 21:49 424Transient:(瞬时状态)内存中一个对象,没有相对应 的 ... -
ssh整合所需lib包目录
2015-03-16 11:44 519转载于:http://blog.csdn.ne ... -
tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class
2015-03-13 16:05 713这个问题的出现是由于slf4j-log4j12 jar的变化, ... -
< could not insert: [com.hibernate.*.* >
2015-03-11 09:42 468hibernate.cfg.xml文件配置: <!- ... -
SSH 错误 信息收集
2015-03-06 16:47 671(文章为转载收集整理。。。。。若有不足请指正!!!谢谢。) ... -
log4j
2015-03-05 11:20 418### 设置org.zblog域对应的级别INFO,DEBUG ... -
B/S C/S
2015-03-05 09:27 469<!--文章改写--> C/S结构,即Clie ... -
Swing和AWT
2015-03-05 09:24 494原文出:http://cherry007.iteye.com/ ... -
错误整理。。。。
2015-03-04 17:28 410文件内容截取自:http://www.cnblogs.com/ ... -
MyEclipse 快捷键
2015-03-04 10:51 436MyEclipse 快捷键 自己设置myeclipse快捷键指 ...
相关推荐
SPRING BEAN的销毁 项目文件(ECLIPSE)。 之前各种测试,一直有问题。后来明白,容器啊!容器啊!!!!!容器灰常重要,使用容器促使BEAN的生命周期。 想了两天,这个问题,SPRING从入门到精通这本书真没处理好...
主要介绍了Spring Bean的初始化和销毁,结合实例形式详细分析了Spring Bean的初始化和销毁相关配置、使用方法及操作注意事项,需要的朋友可以参考下
初始化的时候实现的方法 1、通过java提供的@PostConstruct注解; 2、通过实现spring提供的InitializingBean...3、通过spring的xml bean配置或bean注解指定销毁方法,如下面实例的destroyMethod方法通过@bean注解指定。
主要介绍了Spring实战之Bean销毁之前的行为操作,结合实例形式分析了spring在bean销毁之前的行为相关设置与使用技巧,需要的朋友可以参考下
Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml 1.2 实体类JavaBean 1.2.1 User类 1.3 当Scope="singleton"时 1.4 当 Scope="singleton" 且 lazy-init="true" 时 1.5 当scope=...
NULL 博文链接:https://bijian1013.iteye.com/blog/2374256
主要介绍了Spring Bean初始化及销毁多种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及...Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ApplicationContext 中的生命周期进行分析。
一般情况下,我们只是关心如何正确地将Bean装配到容器中,并不关心Ioc容器是如何装配和销毁Bean的过程。但是恰恰有时候,我们需要自定义初始化或销毁Bean的过程,以满足一些“特殊的”需求。比如,数据源在关闭的...
为了便于这些工作的设计,Spring IoC 容器提供了相关的功能,可以让应用定制 Bean 的初始化和销毁过程。 Bean 生命周期 的 init-method 属性或 @Bean 注解的 initMethod 属性),那么将调用该方法。 如果存在与 ...
作者:钱安川(Moxie) 注:后面的内容我将不再完善,但网上的朋友可以帮助...Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22
Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式事务 22 Spring对其它企业应用支持 22 注:后面的内容我将不再完善,但网上的朋友可以帮助完善,只需注明...
Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务。 Spring IOC容器对Bean的生命周期进行管理的过程如下: 通过构造器或工厂方法创建Bean实例 为Bean的属性设置值和对其它Bean的...
同时,本文对Spring Bean的生命周期进行了深入剖析,从创建、依赖注入到销毁的每个阶段都进行了详尽的讲解。透过这些分析,本文不仅为Java开发者提供了对Spring IoC更深层次的理解,也为高效利用Spring框架提供了...
16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...
16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...
3.4.6 指定Bean的初始化方法和销毁方法 18 Bean的初始化和销毁拓展: 18 Spring的IOC总结: 20 3.5 依赖注入(DI) 20 3.5.1 使用构造器注入 20 3.5.2 使用属性setting方法进行注入 21 3.5.3 装配list集合 22 3.5.4 ...
16.4.3在jsf页面中使用springbean 16.4.4在jsf中暴露应用程序环境 16.5spring中带有dwr的支持ajax的应用程序 16.5.1直接web远程控制 16.5.2访问spring管理的beandwr 16.6小结 附录a装配spring a.1下载spring ...
* IOC:spring容器控制对象的生命周期:前提条件:在spring容器中的bean必须是单例的 * 创建 * 方式 * 利用默认的构造函数,如果没有默认的构造函数,会报错 * 利用静态工厂方法 * 利用实例工厂方法 * 时机 *...