Spring-Bean的销毁使用destroy-method()方法无效解决方案(容器!附源码)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

Bean

package com.gc.action;

import java.util.Date;

public class HelloWorld {

	private String msg=null;//该变量用来存储字符串
	private Date date=null;//该变量用来存储日期
//	public HelloWorld(String msg)
//	{
//		    this.msg=msg;	
//	}
//	
//	public HelloWorld()//这个必须写,否则不能转到上面的那个
//	{
//	    this.msg=msg;	
//	}
	//初始化
	public void init(){
		this.msg="HelloWorld";
		this.date=new Date();
	}
	
	//销毁
	public void cleanup(){
		this.msg="";
		this.date=null;
		System.out.println("您销毁了msg"+this.msg+"和date"+this.date);
	}
	
	//设定变量msg的set方法
	public void setMsg(String msg) {
		this.msg=msg;
	}
	
	//获取变量msg的get方法
	public String getMsg() {
		return this.msg;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	
	
	
	
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定义一个Bean-->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld" init-method="init" destroy-method="cleanup">
    <!--将其变量msg通过依赖注入-->
 

    
    </bean>
    

</beans>


经过修改,原来是容器的问题!容器需要关闭是前提条件,SPRING BEAN的生命周期,SPRING的单例模式,动态代理,反射机制,一定要懂得。写中间件的经历就会特别有助于理解这些框架性的东西:


修改后的项目地址如下:http://download.csdn.net/detail/opzoonzhuzhengke/4156763

http://download.csdn.net/detail/opzoonzhuzhengke/4156763



修改后的代码:

BEAN:

package com.gc.action;

import java.util.Date;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;

public class HelloWorld 
//implements InitializingBean,DisposableBean
{

	private String msg=null;//该变量用来存储字符串
	private Date date=null;//该变量用来存储日期
	


	
	public void afterPropertiesSet() {
		// TODO Auto-generated method stub
		this.msg="HelloWorld";
		this.date=new Date();
		System.out.println("2000");
	}
	
	
	public void cleanup() {
		// TODO Auto-generated method stub
		this.msg="";
		this.date=null;
	    System.out.println("您销毁了msg"+this.msg+"和date"+this.date);  
	}
	
	public HelloWorld(String msg)
	{
		    this.msg=msg;	
	}
	
	public HelloWorld()//这个必须写,否则不能转到上面的那个
	{
	    this.msg=msg;	
	}
	
	//设定变量msg的set方法
	public void setMsg(String msg) {
		this.msg=msg;
	}
	
	//获取变量msg的get方法
	public String getMsg() {
		return this.msg;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}


	
}


config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--定义一个Bean-->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld" init-method="afterPropertiesSet" destroy-method="cleanup">
    <!--将其变量msg通过依赖注入-->
 

    
    </bean>
    

</beans>


测试程序:

package com.gc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gc.action.HelloWorld;


public class TestHelloWorld {
    public static void main(String[] args)
    {
    	//通过ApplicationContext来获取Spring文件的配置
    	AbstractApplicationContext actx=new ClassPathXmlApplicationContext("config.xml");
    
    	
        //通过Bean的id来获取Bean
    	HelloWorld helloworld=(HelloWorld)actx.getBean("HelloWorld");
    	
    	//打印输出
    	System.out.println(helloworld.getMsg()+" "+helloworld.getDate());
    	
    	actx.close();
    	
    }
}


最重要的输出部分:

2000
HelloWorld Tue Mar 20 15:36:46 CST 2012
您销毁了msg和datenull






目录
相关文章
|
2天前
|
Java 应用服务中间件 测试技术
深入探索Spring Boot Web应用源码及实战应用
【5月更文挑战第11天】本文将详细解析Spring Boot Web应用的源码架构,并通过一个实际案例,展示如何构建一个基于Spring Boot的Web应用。本文旨在帮助读者更好地理解Spring Boot的内部工作机制,以及如何利用这些机制优化自己的Web应用开发。
14 3
|
13天前
|
Java 测试技术 开发者
Spring IoC容器通过依赖注入机制实现控制反转
【4月更文挑战第30天】Spring IoC容器通过依赖注入机制实现控制反转
22 0
|
1天前
|
监控 Java 应用服务中间件
Spring Boot 源码面试知识点
【5月更文挑战第12天】Spring Boot 是一个强大且广泛使用的框架,旨在简化 Spring 应用程序的开发过程。深入了解 Spring Boot 的源码,有助于开发者更好地使用和定制这个框架。以下是一些关键的知识点:
13 6
|
5天前
|
存储 前端开发 Java
Spring Boot自动装配的源码学习
【4月更文挑战第8天】Spring Boot自动装配是其核心机制之一,其设计目标是在应用程序启动时,自动配置所需的各种组件,使得应用程序的开发和部署变得更加简单和高效。下面是关于Spring Boot自动装配的源码学习知识点及实战。
13 1
|
6天前
|
传感器 人工智能 前端开发
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
智慧校园电子班牌,坐落于班级的门口,适合于各类型学校的场景应用,班级学校日常内容更新可由班级自行管理,也可由学校统一管理。让我们一起看看,电子班牌有哪些功能呢?
52 4
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
|
7天前
|
Java Spring
Spring的@Retryable实现方法重试
`@Retryable`注解用于配置异常重试,参数包括:指定异常类型`value`,额外包含异常`include`,排除异常`exclude`,最大尝试次数`maxAttempts`和回退策略`backoff`。可选地,可以用`retryExceptions`列表替换`value`。当重试失败,可使用`@Recover`注解定义恢复逻辑。
17 1
|
11天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
21 2
|
13天前
|
XML Java 程序员
什么是Spring的IoC容器?
【4月更文挑战第30天】什么是Spring的IoC容器?
20 0
|
4天前
|
监控 Kubernetes Docker
【Docker 专栏】Docker 容器内应用的健康检查与自动恢复
【5月更文挑战第9天】本文探讨了Docker容器中应用的健康检查与自动恢复,强调其对应用稳定性和系统性能的重要性。健康检查包括进程、端口和应用特定检查,而自动恢复则涉及重启容器和重新部署。Docker原生及第三方工具(如Kubernetes)提供了相关功能。配置检查需考虑检查频率、应用特性和监控告警。案例分析展示了实际操作,未来发展趋势将趋向更智能和高效的检查恢复机制。
【Docker 专栏】Docker 容器内应用的健康检查与自动恢复
|
1天前
|
存储 安全 开发者
如何删除 Docker 镜像、容器和卷?
【5月更文挑战第11天】
12 2
如何删除 Docker 镜像、容器和卷?