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 ...
随机推荐
- Win32 程序开发入门:一个最简单的Win32程序
一.什么是 Win32 Win32 是指 Microsoft Windows 操作系统的 32 位环境,与 Win64 都为 Windows 常见环境. 这里再介绍下 Win32 Applicatio ...
- python服务不能在docker容器里运行的问题
在开发过程中,我们将mysql.redis.celery等服务在docker容器里跑,项目在本地运行,便于debug调试 docker-compose -f docker-compose-dev.ym ...
- Python之基本运算符
基本运算符 1.算符运算符 运算符 描述 例子 + 两个对象相加 a+b - 两个对象相减 a-b * 两个数相乘或返回一个被重复若干次的字符串 a*b / 两个数相除 a/b % 取模,返回除法的余 ...
- 基于 HTML5 WebGL 构建智能城市 3D 场景
前言 随着城市规模的扩大,传统的方式很难彻底地展示城市的全貌,但随着 3D 技术的应用,出现了 3D 城市群的方式以动态,交互式地把城市全貌呈现出来.配合智能城市系统,通过 Web 可视化的方式,使得 ...
- Python中model转dict
问题 在query出来的行信息object中有一个dict变量,这个变量存储了字典信息 for u in session.query(User).all(): print u.__dict__ 但是这 ...
- Redis深度历险,全面解析Redis14个核心知识点
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取. 传送门: ...
- CSS @charset规则
定义和用法 @charset规则指定样式表中使用的字符编码.@charset规则必须在样式表中的第一元素,而不是由任何字符之后进行.在外部样式文件中使用.如果@charset定义了多个规则,则仅使用第 ...
- whistle手机调试工具使用简单教程
npm全局安装 npm install -g whistle 全局启动 w2 start 启动之后,输入127.0.0.1:8899 就可以访问到whistle调试界面了: 我们主要常用几个功能: 1 ...
- Kali linux-信息收集-dmitry
信息收集-dmitry DMitry(Deepmagic Information Gathering Tools 深度信息收集工具)是一个kali linux下用C语言写的工具.主要功能为端口扫描,w ...
- CodeWarrior IDE烧写介绍
点击Flash烧写 选择芯片系列 下面将以PPC8548 NOR Flash烧写为例 默认配置文件目录:C:\Program Files (x86)\Freescale\CodeWarrior PA ...