Spring(七):Bean的初始化和销毁(注解实现)

star2017 1年前 ⋅ 333 阅读

  Spring Bean在使用之前或使用之后需要做一些操作,Spring对Bean的生命周期的操作提供了支持。

配置

  1. Java配置方式
    使用@BeaninitMethoddestroyMethod。相当于XML配置的init-methoddestory-method
  2. 注解方式
    利用JSR-250@PostConstruct@PreDestroy
    @PostConstruct:在构造函数执行完后执行。
    @PreDestroy:Bean销毁之前执行。

示例

  1. 导包js4250-api.jar
  2. 使用@Bean形式的Bean

     package com.bean.initAndDestroy;
    
     /**
      * 使用@Bean形式的Bean
      * @author Rocky
      *
      */
    
     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-destroy-method");
         }
     }
    
  3. 使用 JSR250 形式的Bean

     package com.bean.initAndDestroy;
    
     import javax.annotation.PostConstruct;
     import javax.annotation.PreDestroy;
    
     /**
      * 使用JSR250形式的Bean
      * @author Rocky
      *
      */
     public class JSR250WayService {
    
         @PostConstruct
         public void init() {
             System.out.println("jsr250-init-method");
         }
    
         public JSR250WayService() {
             super();
             System.out.println("初始化构造函数-JSR250WayService");
         }
    
         @PreDestroy
         public void destory() {
             System.out.println("jsr250-destroy-method");
         }
     }
    
  4. 配置类

     package com.bean.initAndDestroy;
    
     import org.springframework.context.annotation.Bean;
     import org.springframework.context.annotation.ComponentScan;
     import org.springframework.context.annotation.Configuration;
    
     @Configuration
     @ComponentScan("com.bean.initAndDestroy")
     public class PrePostConfig {
    
         @Bean(initMethod="init", destroyMethod="destroy")
         BeanWayService beanWayService() {
             return new BeanWayService();
         }
    
         @Bean
         JSR250WayService jsr250WayService() {
             return new JSR250WayService();
         }
     }
    
  5. 执行Main类

     package com.bean.initAndDestroy;
    
     import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
     public class MainPrePost {
    
         @SuppressWarnings("unused")
         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();
         }
     }
    
  6. 结果

     初始化构造函数-BeanWayService
     @Bean-init-method
     初始化构造函数-JSR250WayService
     jsr250-init-method
     jsr250-destroy-method
     @Bean-destroy-method
    
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: