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. c/c++概述

    c/c++的学习分为两个部分 一.语言标准 语言标准定义了功能特性和标准库两部分. 功能特性由编译器负责具体实现,比如linux下gcc,windows下Visual Studio 标准库实现依赖于具 ...

  2. 3. java 方法入门

    一.方法定义 1. 定义格式 public static void 方法名称(){ 方法体 } 1. 方法名称:命名和变量一致,小驼峰式 2. 方法体:大括号中可以包含任意条语句 注意事项: 1. 方 ...

  3. Less(5)

    考查点:双查询报错注入 1.先判断注入类型 (1)首先看到要求,要求传一个ID参数,并且要求是数字型的:?id=1 (2)再输入?id=1' 显示报错,报错多了一个单引号 (3)再输入?id=1'' ...

  4. 【TCP/IP网络编程】:01理解网络编程和套接字

    1.网络编程和套接字 网络编程与C语言中的printf函数和scanf函数以及文件的输入输出类似,本质上也是一种基于I/O的编程方法.之所以这么说,是因为网络编程大多是基于套接字(socket,网络数 ...

  5. LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String

    题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...

  6. 【MySQL】MySQL 8.0的SYS视图

    MySQL的SYS视图 MySQL8.0的发展越来越趋同与Oracle,为了更好的监控MySQL的一些相关指标,出现了SYS视图,用于监控. 1.MySQL版本 (root@localhost) [s ...

  7. 支付签名 MD5Util 排序工具类

    package com.skynet.wechat.wxPay.common; import java.security.MessageDigest; import java.util.Iterato ...

  8. int[]里数的个数怎么由输入决定?-----动态数组。

    java中如何创建动态数组?(摘自百度知道) Java动态数组是一种可以任意伸缩数组长度的对象,在Java中比较常用的是ArrayList,ArrayList是javaAPI中自带的java.util ...

  9. C#中对文件进行选择对话框打开和保存对话框进行复制

    场景 通过文件选择对话框选择文件 复制文件到指定路径 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号霸道的程序猿获取编程相关电子书.教 ...

  10. 五个常用的CSS简写

    1,margin/padding. (演示仅为margin,padding同理,需注意的是padding没有auto) 2.background. background: [background-co ...