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. LRU hashMap(拉链) + 双向链表 java实现

    //基于 hash (拉链法) + 双向链表,LRUcache //若改为开放寻址,线性探测法能更好使用cpuCache public class LRU { private class Node { ...

  2. Ovirt 简单配置

    Ovirt是一款开源的虚拟化平台管理 主要组成: 1.OvirtEngine Server 用于管理和分配资源 ,能通过web管理 2.Hosts 提供虚拟化功能,提供CPU资源和内存资源,用于分配给 ...

  3. 201871010116-祁英红《面向对象程序设计(java)》第十周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...

  4. 2019面向对象程序设计(Java) 第16周学习指导及要求

    2019面向对象程序设计(Java)第16周学习指导及要求 (2019.12.13-2019.12.16) 学习目标 (1) 掌握Java应用程序的打包操作: (2) 掌握线程概念: (3) 掌握线程 ...

  5. python直接赋值、浅拷贝、深拷贝的区别

    一:直接赋值 赋值,就是对象的引用,给对象起别名. i = 8j = iprint("值是:",i, "地址:",id(i))print("值是:&q ...

  6. day95_11_28,selenium定位元素,cookies获取

    一.selenium selenium可以将一些资源定位: # 1.find_element_by_id 根据id找 # 2.find_element_by_link_text 根据链接名字找到控件( ...

  7. dva+umi+antd项目从搭建到使用(没有剖验证,不知道在说i什么)

    先创建一个新项目,具体步骤请参考https://www.cnblogs.com/darkbluelove/p/11338309.html 一.添加document.ejs文件(参考文档:https:/ ...

  8. 理解Promise.all,Promise.all与Promise.race的区别,如何让Promise.all在rejected失败后依然返回resolved成功结果

     壹 ❀ 引 我在 es6入门4--promise详解 这篇文章中有详细介绍Promise对象的用法,文章主题更偏向于对于Promise概念的理解与各方法基本使用介绍:而世上一个比较有趣的问题就是,即 ...

  9. Jenkins操作学习 -- 配置及使用

    一.jenkins基本配置 1.在Jenkins首页,点击Manage Jenkins,然后再点击Manage Plugins插件管理,安装必要的插件.这里我只需要安装Git,因为第一次初始化安装没成 ...

  10. commonDispatchException 函数的逆向

    看书中给出的内容: 1:在栈中构建 EXCEPTION_RECORD 结构体 2. 根据函数传递参数逆推得到 "判断先前模式"的反汇编代码