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. 02-Node.js学习笔记-系统模块fs文件操作

    2.1.什么是系统模块 Node 运行环境提供的API,因为这些API都是以模块化的方式进行开发的,所有我们又称Node运行环境提供的API为系统模块 3.1系统模块fs文件操作 //f :file ...

  2. Educational Codeforces Round 76 (Rated for Div. 2)

    传送门 A. Two Rival Students 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/13 22:37:26 */ #incl ...

  3. luoguP1020 导弹拦截

    题意 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国 ...

  4. 2.jenkins 插件

    默认插件 常用插件,视屏推荐(仅供参考 ) 以下是实际 安装的 插件. SSH SCP publisherGit # 新版jenkins默认就安装了.GitLab Git Parameter Pipe ...

  5. python 格式化打印

    #coding=utf-8 import time; start_time = time.time()for a in range(0,1001): for b in range(0,1001): f ...

  6. 给用户提供就医帮助的安卓APP

    经过我们的小组的成员讨论,我们确定了我们小组的项目,即是一款给用户提供就医帮助的安卓APP. 项目计划及功能:计划两个月内团队成员共同开发完成此款APP,此款APP提供预约挂号,名医名院咨询, 就医导 ...

  7. POJ 1724 (分层图最短路)

    ### POJ 1724 题目链接 ### 题目大意: 给你 N 个点 ,M 条有向路,走每条路需要花费 C 元,这段路的长度为 L . 给你 K 元,问你能否从 1 走到 N 点且花费不超过 K 元 ...

  8. 配置文件和sqlplus简单使用

    oracle简单配置文件 数据文件目录 D:\app\inmeditation\oradata\orcl 以.CTL结尾得的文件是数据库的控制文件 以.LOG结尾的是数据库日志文件 以.DBF结尾的是 ...

  9. 直接命令行中执行PHP代码(PHP CLI模式)

    一般执行PHP代码,需要把PHP代码写成一个文件,但是有时候查一些PHP配置没有这个必要,能不能直接执行PHP代码呢? 答案是可以的. 在命令行下,采用CLI方式执行PHP代码,这里列出几个常用的命令 ...

  10. DevExpress 使用 GridControl 时,数据源无法立即更新的问题

    背景 在使用 DevExpress 的 GridControl 为其实现 Checkbox 列,发现如果勾选了三行的数据,在遍历 GridControl 绑定的数据源时 Checkbox 列的数据仅有 ...