Spring Bean的初始化和销毁实例详解

 更新时间:2019年11月05日 12:00:14   作者:cakincqm  
这篇文章主要介绍了Spring Bean的初始化和销毁,结合实例形式详细分析了Spring Bean的初始化和销毁相关配置、使用方法及操作注意事项,需要的朋友可以参考下

本文实例讲述了Spring Bean的初始化和销毁。分享给大家供大家参考,具体如下:

一 点睛

在开发过程中,经常遇到在Bean使用之前或者之后做一些必要的操作,Spring对Bean的生命周期的操作提供了支持。

1 Java配置方式:使用@Bean的initMethod和destroyMethod。

2 注解方式:利用JSR-250的@PostConstruct和@PreDestroy。

二 实战

1 增加JSR250支持

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>jsr250-api</artifactId>
  <version>1.0</version>
</dependency>

2 使用@Bean形式的Bean

package com.wisely.highlight_spring4.ch2.prepost;
public class BeanWayService {
   public void init(){
      System.out.println("@Bean-init-method");
    }
    public BeanWayService() {
      super();
      System.out.println("初始化构造函数-BeanWayService");
    }
    public void destroy(){
      System.out.println("@Bean-destory-method");
    }
}

3 使用JSR250形式的Bean

package com.wisely.highlight_spring4.ch2.prepost;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class JSR250WayService {
  @PostConstruct //在构造函数执行之后执行
  public void init(){
    System.out.println("jsr250-init-method");
  }
  public JSR250WayService() {
    super();
    System.out.println("初始化构造函数-JSR250WayService");
  }
  @PreDestroy //在Bean销毁之前执行
  public void destroy(){
    System.out.println("jsr250-destory-method");
  }
}

4 编写配置类

package com.wisely.highlight_spring4.ch2.prepost;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.prepost")
public class PrePostConfig {
  //initMethod和destroyMethod指定BeanWayService类的
  //init和destroy方法在构造函数之后、Bean销毁之前执行
  @Bean(initMethod="init",destroyMethod="destroy")
  BeanWayService beanWayService(){
    return new BeanWayService();
  }
  @Bean
  JSR250WayService jsr250WayService(){
    return new JSR250WayService();
  }
}

5 编写主类

package com.wisely.highlight_spring4.ch2.prepost;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context =
        new AnnotationConfigApplicationContext(PrePostConfig.class);
    BeanWayService beanWayService = context.getBean(BeanWayService.class);
    JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
    context.close();
  }
}

三 测试

八月 06, 2018 9:53:47 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1f17ae12: startup date [Mon Aug 06 21:53:47 CST 2018]; root of context hierarchy
初始化构造函数-BeanWayService
@Bean-init-method
初始化构造函数-JSR250WayService
jsr250-init-method
八月 06, 2018 9:53:47 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1f17ae12: startup date [Mon Aug 06 21:53:47 CST 2018]; root of context hierarchy
jsr250-destory-method
@Bean-destory-method

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • java Tcp通信客户端与服务器端实例

    java Tcp通信客户端与服务器端实例

    这篇文章主要介绍了java Tcp通信客户端与服务器端,结合完整实例形式详细分析了java基于tcp的网络通信客户端与服务器端具体实现技巧,需要的朋友可以参考下
    2020-01-01
  • spring配置文件中util:properties和context:property-placeholder用法

    spring配置文件中util:properties和context:property-placeholder用法

    这篇文章主要介绍了spring配置文件中util:properties和context:property-placeholder用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java静态泛型使用方法实例解析

    Java静态泛型使用方法实例解析

    这篇文章主要介绍了Java静态泛型使用方法实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • SpringBoot3.x版本与Mybatis-Plus不兼容问题

    SpringBoot3.x版本与Mybatis-Plus不兼容问题

    当使用3.x版本的SpringBoot结合Mybatis-Plus时版本不兼容就会报错,本文就来介绍一下这个问题的解决方法,感兴趣的可以了解一下
    2024-03-03
  • java 实现链栈存储的方法

    java 实现链栈存储的方法

    下面小编就为大家带来一篇java 实现链栈存储的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Spring Boot中使用RSocket的示例代码

    Spring Boot中使用RSocket的示例代码

    这篇文章主要介绍了Spring Boot中使用RSocket的示例代码,特别是spring boot 如何帮助抽象出更低级别的RSocket API,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • IDEA安装阿里巴巴编码规范插件的两种方式详解(在线安装和离线安装)

    IDEA安装阿里巴巴编码规范插件的两种方式详解(在线安装和离线安装)

    这篇文章主要介绍了IDEA安装阿里巴巴编码规范插件的两种方式详解(在线安装和离线安装),本文通过截图给大家展示的非常详细,需要的朋友可以参考下
    2021-09-09
  • Java之Springcloud Feign组件详解

    Java之Springcloud Feign组件详解

    这篇文章主要介绍了Java之Springcloud Feign组件详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java 集合的Contains和Remove方法

    Java 集合的Contains和Remove方法

    这篇文章主要介绍了Java 集合Contains和Remove方法的相关资料,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-02-02
  • Java中Map和Set练习项目实例代码

    Java中Map和Set练习项目实例代码

    这篇文章主要给大家介绍了关于Java中Map和Set练习项目的相关资料,首先介绍了如何使用map来统计字符串数组中每个字符串的出现次数,然后讨论了如何使用set来找出只出现一次的数字,最后提出了一个解决坏键盘打字问题的思路,需要的朋友可以参考下
    2024-11-11

最新评论