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的初始化后和销毁前的处理的更多相关文章

  1. 在Spring Boot中加载初始化数据

    文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...

  2. spring boot之 Bean的初始化和销毁(4)

    原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...

  3. Spring Boot 中初始化资源的几种方式

    假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...

  4. Spring Boot 中初始化资源的几种方式(转)

    假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看.今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回 ...

  5. Spring Boot中普通类获取Spring容器中的Bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...

  6. Spring Boot中使用断路器

    断路器本身是电路上的一种过载保护装置,当线路中有电器发生短路时,它能够及时的切断故障电路以防止严重后果发生.通过服务熔断(也可以称为断路).降级.限流(隔离).异步RPC等手段控制依赖服务的延迟与失败 ...

  7. Spring Boot 中配置文件application.properties使用

    一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...

  8. Spring boot(三)在Spring boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  9. Spring Boot 中使用 jpa

    本文原文版权归 CSDN Hgihness 所有,此处为转载+技术收藏,如有再转请自觉于篇头处标明原文作者及出处,这是大家对作者劳动成果的自觉尊重!! 作者:Hgihness 原文:http://bl ...

随机推荐

  1. 数据库TINYINT类型 参数0 mybatis取不到值

    tinyint存储0的奇怪问题  数据库TINYINT类型   参数0  mybatis取不到值 postman 传参 audited   =0          audited   =1  两种情况 ...

  2. java8-08-创建stream流

    为什么用stream                             应用函数式编程  配合Lamdba表达式 更快操作集合类 数组      什么是 stream               ...

  3. 201871010107-公海瑜《面向对象程序设计(java)》第一周学习总结

    201871010107-公海瑜<面向对象程序设计(java)>第一周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/   ...

  4. python中copy()和deepcopy()详解

    **首先直接上结论: —–我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在.所以改变原有被复制对象不会对已经复制出来的新对象产生影响.—–而浅复制并不会产生一个独立的 ...

  5. Go package: strings

    Go strings Go 的 strings 包中包含许多处理字符串的函数 官方文档:https://golang.org/pkg/strings/ 前缀.后缀 判断字符串前缀.后缀 // 判断字符 ...

  6. C语言解决汉诺塔问题!

    很难受,看了很多资料才明白..... 对这个问题分析,发现思路如下:有n个黄金盘,要先把n-1个弄到B柱上,再把第n个弄到C柱上,然后把n-1个借助A柱弄到C柱上. 实现的函数如下: void f(i ...

  7. C语言验证哥德巴赫猜想

    #include<stdio.h>int f(int x);int main(void){    int n,i;  scanf("%d",&n);  for( ...

  8. Ubuntu 修改默认编辑器

    sudo update-alternatives --config editor

  9. Numpy数值类型与数值运算-03

    什么是NumPy? NumPy是Python中科学计算的基本软件包.它是一个Python库,提供多维数组对象,各种派生对象(例如蒙版数组和矩阵) 以及各种例程,用于对数组进行快速操作,包括数学,逻辑, ...

  10. Docker学习——基本使用

    最近公司项目要用docker部署,第一次接触,记录一下,方便使用时查阅. 你有没有遇到过这种情况,在本地运行良好的代码,在另一台电脑或者另一个环境里一堆bug,可以说是水土不服,本质上是两个电脑的运行 ...