06点睛Spring4.1-Bean的初始化和销毁
6.1 Initialization和Destruction
- spring对bean初始化的时候和销毁时候进行某些操作提供了支持
- 利用@Bean的initMethod和destroyMethod(和xml配置的init-method和destory-method相同)
- 利用JSR-250的@PostConstruct和@PreDestroy
6.2 示例
6.2.1 @Bean形式的Initialization和Destruction
6.2.1.1 新建服务java类
package com.wisely.prepost; public class BeanWayService {
public void init(){
System.out.println("init-method-bean");
}
public BeanWayService() {
super();
System.out.println("初始化构造函数-bean");
}
public void destroy(){
System.out.println("destory-method-bean");
} }
6.2.1.2 新建配置java类
package com.wisely.prepost; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanWayConfig {
@Bean(initMethod="init",destroyMethod="destroy")
public BeanWayService beanWayService(){
return new BeanWayService();
}
}
测试
package com.wisely.prepost; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.prepost");
context.close(); } }
输出结果
初始化构造函数-bean
init-method-bean
destory-method-bean
6.2.2 JSR-250形式的Initialization和Destruction
6.2.2.1 添加jsr250-api到maven依赖
添加如下到pom.xml
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
6.2.2.2 添加jsr250形式的服务类
package com.wisely.prepost; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.stereotype.Service; @Service
public class JSR250WayService {
@PostConstruct
public void init(){
System.out.println("init-method-annotation");
}
public JSR250WayService() {
super();
System.out.println("初始化构造函数-annotation");
}
@PreDestroy
public void destroy(){
System.out.println("destory-method-annotation");
}
}
6.2.2.3 测试
package com.wisely.prepost; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wisely.prepost");
context.close(); } }
输出结果
初始化构造函数-annotation
init-method-annotation
destory-method-annotation
06点睛Spring4.1-Bean的初始化和销毁的更多相关文章
- Spring3实战第二章第一小节 Spring bean的初始化和销毁三种方式及优先级
Spring bean的初始化和销毁有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 优先级第二通过 <bean& ...
- Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)
一.Bean的初始化和销毁 在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持.在使用Java配置和注解配置下提供如下两种方式: ...
- spring boot之 Bean的初始化和销毁(4)
原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...
- 四、Srping之Bean的初始化和销毁
Srping之Bean的初始化和销毁方法 通常,bean的初始化和销毁方法我们有三个地方可以入手,分别是: 自定义初始化,销毁方法 实现spring提供的InitializingBean(初始化逻辑) ...
- 12、生命周期-@Bean指定初始化和销毁方法
12.生命周期-@Bean指定初始化和销毁方法 Bean的生命周期:创建->初始化->销毁 容器管理bean的生命周期 我们可以自定义初始方法和销毁方法,容器在bean进行到当期那生命周期 ...
- 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式
bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...
- Spring bean 实现初始化、销毁方法的方式及顺序
Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...
- spring bean的初始化以及销毁
spring bean初始化或销毁时执行某些方法,有很多使用场景.比如初始化时,启动bean中的线程池.销毁时释放资源,个人比较喜欢实现InitializingBean和 DisposableBean ...
- Spring bean的初始化及销毁
Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...
- @Bean 指定初始化和销毁方法
bean 的生命周期 bean 的创建 --> 初始化 --> 销毁 ioc 容器管理 bean 的声明周期 可以自定义初始化和销毁方法 构造器( 对象创建 )被调用时机 单实例:在容器启 ...
随机推荐
- ES code study
pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="http ...
- ES安装手册
http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.htmlhttps://githu ...
- 课标2-2-1-3 :MMU配置与使用
void create_page_table(void){ unsigned long *ttb = (unsigned long *)0x20000000; unsigned long vaddr, ...
- Comet OJ - Contest #9 & X Round 3题解
传送门 \(A\) 咕咕 typedef long long ll; int a1,a2,n,d;ll res; int main(){ scanf("%d%d%d",&a ...
- 【Python】安装MySQLdb模块centos 6.1 宝塔Linux面板 MySQL5.6
[Python]安装MySQLdb模块centos 6.1 宝塔Linux面板 MySQL5.6 总之是各种坑 先说一下,宝塔安装在centos 6.1 i368 也就是32位系统上的方法 https ...
- Pytest权威教程15-运行Nose用例
目录 运行Nose用例 使用方法 支持的nose风格 不支持的习语/已知问题 返回: Pytest权威教程 运行Nose用例 Pytest基本支持运行Nose框架格式的测试用例. 使用方法 后安装py ...
- Android中显式意图和隐式意图的区别
1.显式意图 可以直接通过名称开启指定的目标组件: 通过构造方法Intent(Context packageContext,class<?>cls)来实现. button_1 = (But ...
- [Shell]利用JS文件反弹Shell
0x01 模拟环境 攻击: kali ip: 192.168.248.132 测试: windows 7 x64 ip: 192.168.248.136 0x02 工具地址 https://githu ...
- ubuntu之路——day10.6 如何理解人类表现和超过人类表现
从某种角度来说,已知的人类最佳表现其实可以被当做贝叶斯最优错误,对于医学图像分类可以参见下图中的例子. 那么如何理解超过人类表现,在哪些领域机器已经做到了超越人类呢?
- Kafka的Topic、Partition和Message
Kafka的Topic和Partition Topic Topic是Kafka数据写入操作的基本单元,可以指定副本 一个Topic包含一个或多个Partition,建Topic的时候可以手动指定Par ...