zoukankan      html  css  js  c++  java
  • 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 实战 》 王云飞 著

    请关于一下啦^_^

    微信公众号

  • 相关阅读:
    Selenium+java
    小白学习安全测试(一)——Http协议基础
    解决chrome运行报错unknown error: cannot get automation extension
    Eclipse 中 不能创建 Dynamic web project
    Jmeter遇到Connection reset by peer的解决方法
    用Java检测远程主机是否能被连接
    Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息
    jenkins的svn路径中文问题
    MySql的存储引擎介绍
    Netty SSL性能调优
  • 原文地址:https://www.cnblogs.com/haloujava/p/11190952.html
Copyright © 2011-2022 走看看