Spring配置文件中bean标签中init-method和destroy-method和用注解方式配置
Person类:
public class Person {
private int i = 0;
public Person(){
System.out.println("实例化一个对象");
}
public void init(){
System.out.println("调用初始化方法....");
}
public void destory222(){
System.out.println("调用销毁化方法....");
}
}
applicationContext.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-2.5.xsd">
<!-- 配置初始化方法和销毁方法,但是如果要销毁方法生效scope="singleton" -->
<bean id="person" class="com.xxc.initAndDestory.domain.Person" scope="singleton" lazy-init="false" init-method="init" destroy-method="destory"></bean>
</beans>
测试类:
public class Test {
public static void main(String[] args) {
//如果要调用销毁方法必须用子类来声明,而不是ApplicationContext,因为ApplicationContext没有close()
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initAndDestory/applicationContext.xml");
Person p1 = (Person)ac.getBean("person");
Person p2 = (Person)ac.getBean("person");
ac.close();
}
}
如果用注解方式配置:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<!-- scope默认是 prototype:getBean()一次创建一个实例-->
<bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>
</beans>
Person类:
public class Person {
private int i = 0;
public Person(){
System.out.println("实例化一个对象");
}
@PostConstruct //初始化方法的注解方式 等同与init-method=init
public void init(){
System.out.println("调用初始化方法....");
}
@PreDestroy //销毁方法的注解方式 等同于destory-method=destory222
public void destory(){
System.out.println("调用销毁化方法....");
}
}
Spring配置文件中bean标签中init-method和destroy-method和用注解方式配置的更多相关文章
- 【Spring源码解读】bean标签中的属性(二)你可能还不够了解的 abstract 属性和 parent 属性
abstract 属性说明 abstract 在java的语义里是代表抽象的意思,用来说明被修饰的类是抽象类.在Spring中bean标签里的 abstract 的含义其实也差不多,表示当前bean是 ...
- 【Spring源码解读】bean标签中的属性
说明 今天在阅读Spring源码的时候,发现在加载xml中的bean时,解析了很多标签,其中有常用的如:scope.autowire.lazy-init.init-method.destroy-met ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- java代码中init method和destroy method的三种使用方式
在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...
- 关于django中input标签中file类型以及开路由
0825自我总结 关于django中input标签中file类型 1.input提交图片实时展示 <img src="/static/img/default.png" wid ...
- HTML中Meta标签中http-equiv属性小结
HTML中Meta标签中http-equiv的用法: <meta http-equiv="这里是参数" content="这里是参数值"> 1.Ex ...
- HTML中Meta标签中http-equiv属性
HTML中Meta标签中http-equiv的用法: <meta http-equiv="这里是参数" content="这里是参数值"> 1.Ex ...
- spring(二、bean生命周期、用到的设计模式、常用注解)
spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
随机推荐
- List<Activity> lists的关闭finish()
public class App extends Application { private static List<Activity> lists = new ArrayList< ...
- java基础(一)注释
注释的三方方式: 1.多行注释 /* 多行注释01 多行注释02 多行注释03 */
- Python爬虫开发与项目实战pdf电子书|网盘链接带提取码直接提取|
Python爬虫开发与项目实战从基本的爬虫原理开始讲解,通过介绍Pthyon编程语言与HTML基础知识引领读者入门,之后根据当前风起云涌的云计算.大数据热潮,重点讲述了云计算的相关内容及其在爬虫中的应 ...
- PHP stristr() 函数
实例 查找 "world" 在 "Hello world!" 中的第一次出现,并返回字符串的剩余部分: <?php高佣联盟 www.cgewang.com ...
- IOFFSETOF ICONTAINEROF IQUEUE_ENTRY
#include <iostream> #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #de ...
- P5468 [NOI2019]回家路线 斜率优化 dp
LINK:回家路线 (文化课 oi 双爆炸 对 没学上的就是我.(我错了不该这么丧的. 不过还能苟住一段时间.当然是去打NOI了 这道题去年同步赛的时候做过.不过这里再次提醒自己要认真仔细的看题目 不 ...
- 怎么下载腾讯课堂M3U8格式的视频
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 用过腾讯课堂的小伙伴们可能 ...
- hdu6787(骰子+往回推的传输带问通过方案,dp)
题:http://acm.hdu.edu.cn/showproblem.php?pid=6787 题意:有1~n标号的格子,上面有m个传输带(传送带传的位置要传到之前去,1位置不能有格子)1~11的骰 ...
- 15 张精美动图全面讲解 CORS
前言: 本文翻译自 Lydia Hallie 小姐姐写的
- C# 实现线程的常用几种方式
前言 在各个开发语言中,线程是避免不了的,或许通过表象看不出来,但是真的无处不在.就比如一个Web程序,平时或许只注重增删改查的开发,根本没有编写相关多线程的的代码,但是请求内部的时候,已经分配了对应 ...