1. 程式人生 > >Spring Boot實戰筆記(三)-- Spring常用配置(Bean的初始化和銷毀、Profile)

Spring Boot實戰筆記(三)-- Spring常用配置(Bean的初始化和銷毀、Profile)

div nbsp troy string 實例化 public ive work 初始

一、Bean的初始化和銷毀

  在我們的實際開發的時候,經常會遇到Bean在使用之前或之後做些必要的操作,Spring對Bean的生命周期操作提供了支持。在使用Java配置和註解配置下提供如下兩種方式:

  (1)Java配置的方式:使用 @Bean 的 initMethod 和 destroyMethod(相當於xml配置中的 init-method 和 destroy-method)。

  (2)註解方式:利用JSR-250的 @PostContruct 和 @PreDestroy。

演示:

  1.增加 JSR-250 支持。

<!-- JSR-250 支持 -->
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>jsr250-api</artifactId>
      <version>1.0</version>
    </dependency>

  2.使用 @Bean 形式的Bean。

package com.ecworking.bean;

public class BeanWayService {

    public void init(){
        System.out.println("@Bean-init-method");
    }

    public BeanWayService() {
        super();
        System.out.println("初始化構造函數-BeanWayService");

    }

    private void destroy(){
        System.out.println("@Bean-destroy-method");
    }
}

  3.使用JSR250形式的Bean。

package com.ecworking.bean;

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銷毀之前執行
    private void destroy(){
        System.out.println("@JSR250-destroy-method");
    }
}

  4.配置類。

package com.ecworking.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.ecworking.bean")
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.ecworking.bean;

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();
    }
}

運行結果:

技術分享

二、Profile

  Prifile為不同環境下提供不同不同配置提供了支持(開發環境和生產環境下的配置肯定是不同的,例如,數據庫的配置)。

  1.通過設定Environment的 ActiveProfile來設定當前context需要使用的配置環境。在開發環境中使用@Profile註解類或方法,達到在不同環境下選擇實例化不同的Bean。

  2.通過設定jvm的spring.profile.active參數來設置配置環境。

  3.Web項目設置在Servlet的context parameter中。

  Servlet2.5及以下:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>production</param-value>
    </init-param>
</servlet>

  Servlet3.0及以上

Spring Boot實戰筆記(三)-- Spring常用配置(Bean的初始化和銷毀、Profile)