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 ...
随机推荐
- IntelliJ IDEA:文件的路径本该是”\“,却变成了”¥“
修改字体导致的, 有些字体中是用¥替换掉\的,所以,修改到合适的字体就好了
- Redis(二)Jedis操作Redis
如果测试连接的时候,报下面这个异常,可以参考下面的博客进行处理: Exception in thread "main" redis.clients.jedis.exceptions ...
- Vue开启gzip压缩文件
在你的项目使用了Vue的路由懒加载.Vue使用CDN引用项目组件,减少项目体积 后,还是觉得项目加载速度慢,效果不尽如人意的时候,还有一个“瘦身项目”可以完成,那就是利用nginx和webpack来使 ...
- e的存在性证明和计算公式的证明
\(\quad\quad前言\quad\quad\\\) \(此证明,改编自中科大数分教材,史济怀版\\\) \(中科大教材,用的是先固定m,再放大m,跟菲赫金哥尔茨的方法一样.\\\) \(而我这里 ...
- Arduino+温度、湿度传感器
Arduino语言注解Arduino语言是建立在C/C++基础上的,其实也就是基础的C语言,Arduino语言只不过把AVR单片机(微控制器)相关的一些参数设置都函数化,不用我们去了解他的底层,让我们 ...
- PHP set_exception_handler() 函数
定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行期间的用户自己的异常处理方法. 该函数返回旧的异常处理程序,如果失败则返回 NULL. ...
- 一本通 高手训练 1782 分层图 状压dp
LINK:分层图 很精辟的一道题 写的时候没带脑子 导致搞了半天不知道哪错了. 可以想到状压每次到某一层的状态 然后这个表示方案数 多开一维表示此时路径条数的奇偶即可. 不过显然我们只需要知道路径条数 ...
- ElasticSearch 基本概念 and 索引操作 and 文档操作 and 批量操作 and 结构化查询 and 过滤查询
基本概念 索引: 类似于MySQL的表.索引的结构为全文搜索作准备,不存储原始的数据. 索引可以做分布式.每一个索引有一个或者多个分片 shard.每一个分片可以有多个副本 replica. 文档: ...
- intel:spectre&Meltdown侧信道攻击(四)—— cache mapping
前面简单介绍了row hammer攻击的原理和方法,为了更好理解这种底层硬件类攻击,今天介绍一下cpu的cache mapping: 众所周知,cpu从内存读数据,最开始用的是虚拟地址,需要通过分页机 ...
- SparkSQL JDBC和JDBCServer区别
注意SparkSQL JDBC和SparkSQL JDBCSever是完全两种不同的场景. SparkSQL JDBC SparkSQL可以使用JDBC的方式访问其他数据库,和普通非spark程序访问 ...