首页 技术 正文
技术 2022年11月18日
0 收藏 360 点赞 2,642 浏览 2348 个字

当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态。当bean不再需要,并且从容器中移除时,需要做一些清除工作。为了定义安装和拆卸一个 bean,我们只要声明init-method 和/或 destroy-method 参数。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。

编写HelloWorld.java

 package com.example.spring; public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
} public void getMessage(){
System.out.println("Your Message : " + message);
}
//定义初始化方法
public void init(){
System.out.println("Bean is going through init.");
}
//定义销毁方法
public void destroy(){
System.out.println("Bean will destroy now.");
}
}

编写Beans.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloWorld"
class="com.example.spring.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World"></property>
</bean>
</beans>

编写Application.java

 package com.example.spring; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
HelloWorld obj = (HelloWorld)context.getBean("helloWorld");
obj.getMessage();
//registerShutdownHook关闭hook,确保正常关闭,并调用destroy方法
context.registerShutdownHook();
}
}

运行输出

Bean is going through init.
Your Message : Hello World!
Bean will destroy now.

默认的初始化和销毁方法

如果你有太多具有相同名称的初始化或者销毁方法的 Bean,那么你不需要在每一个 bean 上声明初始化方法和销毁方法。框架使用 元素中的 default-init-method 和 default-destroy-method 属性提供了灵活地配置这种情况,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-init-method="init"
default-destroy-method="destroy"
> <bean id="helloWorld"
class="com.example.spring.HelloWorld">
<property name="message" value="Hello World"></property>
</bean>
</beans>

运行输出

Bean is going through init.
Your Message : Hello World!
Bean will destroy now.
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,961
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,485
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,746
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,780