Spring Boot 中Bean的初始化后和销毁前的处理
Spring 对Bean的生命周期的操作提供了支持。具体实现又两种方式
1.使用@Bean 中的 initMethod 和 destroyMethod
2.注解方式 利用JSR-250 中的@PostConstruct 和 @PreDesctroy
两种方式的具体用法如下
1.创建功能,为了快速完成项目构建,我们使用 https://start.spring.io/ 网址来快速生成项目

2.引入Jsr250 支持
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
3.使用@Bean 的形式
package com.lvlvstart.example.service;
public class BeanWayService {
public void init(){
System.out.println("@Bean-init-method - BeanWayService");
}
public BeanWayService(){
super();
System.out.println("Init constructor method - BeanWayService");
}
public void destroy(){
System.out.println("@Bean-destory-method - BeanWayService");
}
}
4.使用Jsr250的形式
package com.lvlvstart.example.service; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; public class Jsr250Service { @PostConstruct
public void init(){
System.out.println("@Bean-init-method-Jsr250Service");
} public Jsr250Service(){
super();
System.out.println("Init constructor method - Jsr250Service");
} @PreDestroy
public void destroy(){
System.out.println("@Bean-destory-method-Jsr250Service");
} }
5.配置类
package com.lvlvstart.example.config; import com.lvlvstart.example.service.BeanWayService;
import com.lvlvstart.example.service.Jsr250Service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.lvlvstart.example")
public class Config { @Bean(initMethod = "init" , destroyMethod = "destroy")
BeanWayService beanWayService(){
return new BeanWayService();
} @Bean
Jsr250Service jsr250Service(){
return new Jsr250Service();
} }
6.启动类
package com.lvlvstart.example; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ExampleApplication { public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
} }
参考资料: 《Spring Boot 实战 》 王云飞 著
Spring Boot 中Bean的初始化后和销毁前的处理的更多相关文章
- 在Spring Boot中加载初始化数据
文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...
- spring boot之 Bean的初始化和销毁(4)
原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...
- Spring Boot 中初始化资源的几种方式
假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...
- Spring Boot 中初始化资源的几种方式(转)
假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...
- Spring Boot中普通类获取Spring容器中的Bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...
- Spring Boot中使用断路器
断路器本身是电路上的一种过载保护装置,当线路中有电器发生短路时,它能够及时的切断故障电路以防止严重后果发生.通过服务熔断(也可以称为断路).降级.限流(隔离).异步RPC等手段控制依赖服务的延迟与失败 ...
- Spring Boot 中配置文件application.properties使用
一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...
- Spring boot(三)在Spring boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
- Spring Boot 中使用 jpa
本文原文版权归 CSDN Hgihness 所有,此处为转载+技术收藏,如有再转请自觉于篇头处标明原文作者及出处,这是大家对作者劳动成果的自觉尊重!! 作者:Hgihness 原文:http://bl ...
随机推荐
- 0day2安全——笔记1
第一章 PE和内存之间的映射 节偏移 文件偏移地址(File Offset Address):数据在PE文件中的地址 装载地址(Image Base):PE装入内存的基地址 虚拟内存地址(Virtua ...
- java 通过Qrcode生成二维码添加图片logo和文字描述
/** * 二维码创建 * @author yhzm * */ public class printServiceImpl extends BaseService { public void barC ...
- Javascript是如何工作的?
作为一个前端开发者或者全栈开发者,一定非常熟练Javascript.程序员社区Stack Overflow的调查结果显示,Javascript是最常用的编程语言,连续多年排在第一名.发明Javascr ...
- Noip2017Day1T3 逛公园
题目链接 problem 一个有向无重边自环图,设\(D\)为从\(1\)号点走到\(n\)号点的最短距离.问有多少条从\(1\)到\(n\)的路径长度不超过\(D+K\).\(K\)为给定的值,且\ ...
- Linux 下编写一个 PHP 扩展
假设需求 开发一个叫做 helloWord 的扩展. 扩展里有一个函数,helloWord(). echo helloWord('Tom'); //返回:Hello World: Tom 本地 ...
- Docker的入门及常用命令
Docker入门及常用命令 1. 各个容器之间是相互隔离状态: 这样减少了我们软件之间的影响. 2. docker是os层虚拟化架构的一种产品体现, os层虚拟化架构出来的操作系统需要和宿主机操作系统 ...
- jsp页面科学计数法显示问题的解决办法
在JSP页面中返回的double类型的数值,如果数值过大,就会被显示成科学计数法. 这个问题通常出现在表单填写的input中,因此是必须要解决的问题. JSTL提供了标准的数值格式化标签,使用上非常简 ...
- Redis系列---redis简介01
一. 本章我们将用简短的几句话来帮助你快速的了解什么是redis,初学者不必深究 1 Redis简介 Remote Dictionary Server(Redis)是一个开源的使用ANSI C语言编写 ...
- hive 标准hql建表语法格式
一.标准hql建表语法格式1.官方标准语法:(hql不区分大小写,下面[]里的属性是可选属性) 具体参考官网界面:HIVE官网建表说明文档 CREATE [EXTERNAL] TABLE [IF NO ...
- 易优CMS:【小白学标签】之empty的基础用法
[基础用法] 名称:empty 功能:判断某个变量是否为空,可以嵌套到任何标签里面使用,比如:channel.type等 语法: {eyou:empty name='$eyou.field.seo_t ...