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 ...
随机推荐
- Druid-代码段-1-1
所属文章:池化技术(一)Druid是如何管理数据库连接的? 本代码段对应主流程1,具体用来获取一个连接: lic DruidPooledConnection getConnection(long ma ...
- python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作
pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...
- AcWing 799. 最长连续不重复子序列
网址 https://www.acwing.com/solution/AcWing/content/2069/ 题目描述给定一个长度为n的整数序列,请找出最长的不包含重复数字的连续子序列,输出它的长 ...
- acwing 902. 最短编辑距离
地址 https://www.acwing.com/problem/content/904/ 给定两个字符串A和B,现在要将A经过若干操作变为B,可进行的操作有: 删除–将字符串A中的某个字符删除. ...
- HTML网页自动跳转(重定向)
HTML网页自动跳转(重定向) meta <head> <meta http-equiv="refresh" content="5;url=https: ...
- docker /var/lib/docker/aufs/mnt 目录满了怎么清理
1.创建脚本文件 vi cleandocker.sh 内容如下: #!/bin/sh echo "==================== start clean docker contai ...
- 【翻译】spring-data 之JdbcTemplate 使用
文档 Jdbc的使用 基础的代码结构: 一个Application作为入口.IUserRepository和UserRepository作为具体的实现.applicationContext.xml定义 ...
- .NET Core 中读取 Request.Headers 的姿势
Request.Headers 的类型是 IHeaderDictionary 接口,对应的实现类是 HeaderDictionary ,C# 实现源码见 HeaderDictionary.cs . H ...
- Spring案例--打印机
目录: 1.applicationContext.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...
- Groovy元编程应用之自动生成订单搜索接口测试用例集
背景 在 "Groovy元编程简明教程" 一文中,简明地介绍了 Groovy 元编程的特性. 那么,元编程可以应用哪些场合呢?元编程通常可以用来自动生成一些相似的模板代码. 在 & ...