欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot初始化加載配置的八種方式總結(jié)

 更新時(shí)間:2024年12月06日 10:38:22   作者:獨(dú)行客-編碼愛(ài)好者  
在日常開(kāi)發(fā)時(shí),我們常常需要 在SpringBoot應(yīng)用啟動(dòng)時(shí)執(zhí)行某一段邏輯,如獲取一些當(dāng)前環(huán)境的配置或變量、向數(shù)據(jù)庫(kù)寫(xiě)入一些初始數(shù)據(jù)或者連接某些第三方系統(tǒng),確認(rèn)對(duì)方可以工作,那么在實(shí)現(xiàn)初始化邏輯代碼時(shí)就需要小心了,所以本文介紹了SpringBoot初始化加載配置的方式
  • @PostConstruct 注解
  • InitializingBean 接口
  • @Bean initMethod方法
  • 構(gòu)造器注入
  • ApplicationListener
  • CommandLineRunner
  • ApplicationRunner
  • SmartLifecycle 

序號(hào)

初始化加載方式

執(zhí)行時(shí)機(jī)

1

@PostConstruct 注解(在方法加注解)

Bean對(duì)象初始化完成后執(zhí)行( 該方法會(huì)在所有依賴字段注入后才執(zhí)行 

2

構(gòu)造器注入(構(gòu)造方法加  @Autowired注解 

Bean對(duì)象初始化完成后執(zhí)行( 該方法會(huì)在所有依賴字段注入后才執(zhí)行 

3

InitializingBean 接口(繼承 InitializingBean接口,并實(shí)現(xiàn) afterPropertiesSet()這個(gè)方法)

Bean對(duì)象初始化完成后執(zhí)行( 該方法會(huì)在所有依賴字段注入后才執(zhí)行 ) 3和4是一樣的,實(shí)現(xiàn)方式不同

4

@Bean initMethod方法

Bean對(duì)象初始化完成后執(zhí)行( 該方法會(huì)在所有依賴字段注入后才執(zhí)行  3和4是一樣的,實(shí)現(xiàn)方式不同

5

SmartLifecycle 接口(繼承SmartLifecycle 接口),并實(shí)現(xiàn)start() 方法

SmartLifecycle 的執(zhí)行時(shí)機(jī)是在 Spring 應(yīng)用上下文刷新完成之后,即所有的 Bean 都已經(jīng)被實(shí)例化和初始化之后。

6

ApplicationListener(繼承  ApplicationListener接口,并實(shí)現(xiàn)onApplicationEvent()方法  與方法上加 @EventListener的效果一樣 

所有Bean初始化完成后才會(huì)執(zhí)行方法

7

CommandLineRunner 繼承 CommandLineRunner,繼承 run() 方法 

應(yīng)用啟動(dòng)后執(zhí)行

8

ApplicationRunner( 繼承 CommandLineRunner,繼承 run() 方法 

應(yīng)用啟動(dòng)后執(zhí)行

背景

在日常開(kāi)發(fā)時(shí),我們常常需要 在SpringBoot 應(yīng)用啟動(dòng)時(shí)執(zhí)行某一段邏輯,如下面的場(chǎng)景:

  • 獲取一些當(dāng)前環(huán)境的配置或變量

  • 向緩存數(shù)據(jù)庫(kù)寫(xiě)入一些初始數(shù)據(jù)

  • 連接某些第三方系統(tǒng),確認(rèn)對(duì)方可以工作

在實(shí)現(xiàn)這些功能時(shí),我們可能會(huì)遇到一些"坑"。 為了利用SpringBoot框架的便利性,我們不得不將整個(gè)應(yīng)用的執(zhí)行控制權(quán)交給容器,于是造成了大家對(duì)于細(xì)節(jié)是一無(wú)所知的。那么在實(shí)現(xiàn)初始化邏輯代碼時(shí)就需要小心了,比如,我們并不能簡(jiǎn)單的將初始化邏輯在Bean類的構(gòu)造方法中實(shí)現(xiàn),類似下面的代碼:

@Component 
public class InvalidInitExampleBean { 
    @Autowired 
     private Environment env; 
    
    public InvalidInitExampleBean() { 
    
        env.getActiveProfiles(); 
    } 
}

注意:這里,我們?cè)贗nvalidInitExampleBean的構(gòu)造方法中試圖訪問(wèn)一個(gè)自動(dòng)注入的env字段,當(dāng)真正執(zhí)行時(shí),你一定會(huì)得到一個(gè)空指針異常(NullPointerException)。原因在于,當(dāng)構(gòu)造方法被調(diào)用時(shí),Spring上下文中的Environment這個(gè)Bean很可能還沒(méi)有被實(shí)例化,同時(shí)也仍未注入到當(dāng)前對(duì)象,所以并不能這樣進(jìn)行調(diào)用。

下面,我們來(lái)看看在SpringBoot中實(shí)現(xiàn)"安全初始化"的一些方法:

一、@PostConstruct 注解

@PostConstruct 注解其實(shí)是來(lái)自于 javax的擴(kuò)展包中(大多數(shù)人的印象中是來(lái)自于Spring框架),它的作用在于聲明一個(gè)Bean對(duì)象初始化完成后執(zhí)行的方法。

來(lái)看看它的原始定義:

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

也就是說(shuō),該方法會(huì)在所有依賴字段注入后才執(zhí)行,當(dāng)然這一動(dòng)作也是由Spring框架執(zhí)行的。 

示例:

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Arrays;
 
/**
 * @Description TODO
 * @date 2024/12/5 11:19
 * @Version 1.0
 * @Author gezongyang
 */
@Component
@Slf4j
public class InvalidInitExampleBean {
 
    @Autowired
    private Environment environment;
 
    /**
     * 該方法會(huì)在所有依賴字段(environment)注入后才執(zhí)行,當(dāng)然這一動(dòng)作也是由Spring框架執(zhí)行的。
     */
    @PostConstruct
    public void init() {
        //environment 已經(jīng)注入
        log.info("@PostConstruct execute:{}",Arrays.asList(environment.getDefaultProfiles()));
    }
}

二、實(shí)現(xiàn) InitializingBean 接口

InitializingBean 是由Spring框架提供的接口,其與@PostConstruct注解的工作原理非常類似。

如果不使用注解的話,你需要讓Bean實(shí)例繼承 InitializingBean接口,并實(shí)現(xiàn)afterPropertiesSet()這個(gè)方法。

示例:

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
 
/**
 * @Description 繼承 InitializingBean接口,并實(shí)現(xiàn)afterPropertiesSet()
 *  afterPropertiesSet() 會(huì)在所有依賴的字段(environment)注入后才執(zhí)行
 * @date 2024/12/5 11:37
 * @Version 1.0
 * @Author gezongyang
 */
@Component
@Slf4j
public class InitializingBeanExampleBean implements InitializingBean {
 
    @Autowired
    private Environment environment;
 
    /**
      * 這個(gè)方法會(huì)在environment注入后執(zhí)行
     */
    @Override
    public void afterPropertiesSet() {
        //environment 已經(jīng)注入
        log.info("InitializingBean execute:{}", Arrays.asList(environment.getDefaultProfiles()));
    }
}

三、@Bean initMethod方法

我們?cè)诼暶饕粋€(gè)Bean的時(shí)候,可以同時(shí)指定一個(gè)initMethod屬性,該屬性會(huì)指向Bean的一個(gè)方法,表示在初始化后執(zhí)行。

示例:

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
 
/**
 * @Description 指定一個(gè)initMethod屬性,該屬性會(huì)指向Bean的一個(gè)方法,表示在初始化后執(zhí)行。
 * @date 2024/12/5 11:37
 * @Version 1.0
 * @Author gezongyang
 */
@Component
@Slf4j
public class InitializingBean {
 
    @Autowired
    private Environment environment;
 
    /**
     * 這個(gè)方法會(huì)在environment注入后執(zhí)行
     */
    public void init() {
        //environment 已經(jīng)注入
        log.info("@Bean initMethod execute:{}", Arrays.asList(environment.getDefaultProfiles()));
    }
 
    /**
     * 這里將initMethod指向init方法,相應(yīng)的我們也需要在Bean中實(shí)現(xiàn)這個(gè)方法:
     * @return
     */
    @Bean(initMethod="init")
    public InitializingBean exBean() {
        return new InitializingBean();
    }
}

四、構(gòu)造器注入

如果依賴的字段在Bean的構(gòu)造方法中聲明,那么Spring框架會(huì)先實(shí)例這些字段對(duì)應(yīng)的Bean,再調(diào)用當(dāng)前的構(gòu)造方法。

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
 
/**
 * @Description 如果依賴的字段在Bean的構(gòu)造方法中聲明,那么Spring框架會(huì)先實(shí)例這些字段對(duì)應(yīng)的Bean,
 * 再調(diào)用當(dāng)前的構(gòu)造方法。
 * @date 2024/12/5 14:11
 * @Version 1.0
 * @Author gezongyang
 */
@Slf4j
@Component
public class LogicInConstructorExampleBean {
    private final Environment environment;
 
    /**
     * LogicInConstructorExampleBean(Environment environment) 調(diào)用在
     * Environment 對(duì)象實(shí)例化之后
     * @param environment
     */
    @Autowired
    public LogicInConstructorExampleBean(Environment environment) {
        //environment實(shí)例已初始化
        this.environment = environment;
        log.info("LogicInConstructor:{}", Arrays.asList(environment.getDefaultProfiles()));
    }
}

五、實(shí)現(xiàn)ApplicationListener 接口

ApplicationListener 是由 spring-context組件提供的一個(gè)接口,主要是用來(lái)監(jiān)聽(tīng) “容器上下文的生命周期事件”。它的定義如下:

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    /**
     * Handle an application event.
     * @param event the event to respond to
     */
    void onApplicationEvent(E event);
}

這里的event可以是任何一個(gè)繼承于ApplicationEvent的事件對(duì)象。 對(duì)于初始化工作來(lái)說(shuō),我們可以通過(guò)監(jiān)聽(tīng)ContextRefreshedEvent這個(gè)事件來(lái)捕捉上下文初始化的時(shí)機(jī)。

示例:

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
/**
 * @Description 當(dāng)所有的Bean都初始化完成后,執(zhí)行onApplicationEvent 方法
 * @date 2024/12/5 14:19
 * @Version 1.0
 * @Author gezongyang
 */
@Slf4j
@Component
public class StartupApplicationListenerExample  implements ApplicationListener<ContextRefreshedEvent> {
    public static int counter;
 
    /**
     * 這里的event可以是任何一個(gè)繼承于ApplicationEvent的事件對(duì)象。
     * 對(duì)于初始化工作來(lái)說(shuō),我們可以通過(guò)監(jiān)聽(tīng)ContextRefreshedEvent這個(gè)事件來(lái)捕捉上下文初始化的時(shí)機(jī)。
     * @param event
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("ApplicationListener run!");
        counter++;
    }
}

在Spring上下文初始化完成后,這里定義的方法將會(huì)被執(zhí)行。 與前面的 InitializingBean 不同的是,通過(guò) ApplicationListener 監(jiān)聽(tīng)的方式是全局性的,也就是當(dāng) 所有 的 Bean 都 初始化完成 后才會(huì)執(zhí)行方法。
Spring 4.2 之后引入了新的 @EventListener注解,可以實(shí)現(xiàn)同樣的效果:

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
/**
 * @Description TODO
 * @date 2024/12/5 14:24
 * @Version 1.0
 * @Author gezongyang
 */
@Slf4j
@Component
public class EventListenerExampleBean {
    public static int counter;
 
    /**
     * 所有的Bean都初始化完成后,執(zhí)行onApplicationEvent方法。
     * @param event
     */
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("@EventListener execute", event.toString());
        counter++;
    }
}

六、實(shí)現(xiàn) CommandLineRunner 接口

SpringBoot 提供了一個(gè)CommanLineRunner接口,用來(lái)實(shí)現(xiàn)在應(yīng)用啟動(dòng)后的邏輯控制,其定義如下:

public interface CommandLineRunner {
    /**
     * Callback used to run the bean.
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    void run(String... args) throws Exception;
}

此外,對(duì)于多個(gè)CommandLineRunner的情況下可以使用@Order注解來(lái)控制它們的順序。

案例:

1 定義一個(gè)CommandDemo實(shí)現(xiàn)CommandLineRunner,并納入到spring容器

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
 * @Description 應(yīng)用啟動(dòng)后的回調(diào)函數(shù),在應(yīng)用啟動(dòng)后執(zhí)行此方法
 * @date 2024/12/5 14:32
 * @Version 1.0
 * @Author gezongyang
 */
@Component
@Slf4j
public class CommandDemo implements CommandLineRunner {
    /**
     * 此方法在應(yīng)用啟動(dòng)后執(zhí)行
     * @param args
     */
    @Override
    public void run(String... args) {
        log.info("CommandLineRunner run!");
    }
}

2 配置參數(shù),然后執(zhí)行啟動(dòng)類

3 打印結(jié)果:

=====應(yīng)用已經(jīng)啟動(dòng)成功======[aaa,bbb]

七、實(shí)現(xiàn) ApplicationRunner 接口

package org.springframework.boot;
 
 
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
 
 
/**
* Interface used to indicate that a bean should <em>run</em> when it is contained within
* a {@link SpringApplication}. Multiple {@link ApplicationRunner} beans can be defined
* within the same application context and can be ordered using the {@link Ordered}
* interface or {@link Order @Order} annotation.
*
* @author Phillip Webb
* @since 1.3.0
* @see CommandLineRunner
*/
@FunctionalInterface
public interface ApplicationRunner {
 
   /**
    * Callback used to run the bean.
    * @param args incoming application arguments
    * @throws Exception on error
    */
   void run(ApplicationArguments args) throws Exception;
}

與 CommandLineRunner接口類似, Spring boot 還提供另一個(gè)ApplicationRunner 接口來(lái)實(shí)現(xiàn)初始化邏輯。不同的地方在于 ApplicationRunner.run()方法接受的是封裝好的ApplicationArguments參數(shù)對(duì)象,而不是簡(jiǎn)單的字符串參數(shù)。ApplicationArguments是對(duì)參數(shù)做了進(jìn)一步的處理,可以解析key=value形式,我們可以通過(guò)name來(lái)獲取value(而CommandLineRunner只是獲取key=value整體)

package org.springframework.boot;
 
import java.util.List;
import java.util.Set;
/**
* Provides access to the arguments that were used to run a {@link SpringApplication}.
*
* @author Phillip Webb
* @since 1.3.0
*/
public interface ApplicationArguments {
 
   /**
    * Return the raw unprocessed arguments that were passed to the application.
    * @return the arguments
    */
   String[] getSourceArgs();
 
   /**
    * Return the names of all option arguments. For example, if the arguments were
    * "--foo=bar --debug" would return the values {@code ["foo", "debug"]}.
    * @return the option names or an empty set
    */
   Set<String> getOptionNames();
 
   /**
    * Return whether the set of option arguments parsed from the arguments contains an
    * option with the given name.
    * @param name the name to check
    * @return {@code true} if the arguments contain an option with the given name
    */
   boolean containsOption(String name);
 
   /**
    * Return the collection of values associated with the arguments option having the
    * given name.
    * <ul>
    * <li>if the option is present and has no argument (e.g.: "--foo"), return an empty
    * collection ({@code []})</li>
    * <li>if the option is present and has a single value (e.g. "--foo=bar"), return a
    * collection having one element ({@code ["bar"]})</li>
    * <li>if the option is present and has multiple values (e.g. "--foo=bar --foo=baz"),
    * return a collection having elements for each value ({@code ["bar", "baz"]})</li>
    * <li>if the option is not present, return {@code null}</li>
    * </ul>
    * @param name the name of the option
    * @return a list of option values for the given name
    */
   List<String> getOptionValues(String name);
 
   /**
    * Return the collection of non-option arguments parsed.
    * @return the non-option arguments or an empty list
    */
   List<String> getNonOptionArgs();
}

ApplicationArguments可以接收key=value這樣的參數(shù),getOptionNames()方法可以得到key的集合,getOptionValues(String name)方法可以得到value這樣的集合

示例:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
/**
* @Description TODO
* @date 2022/6/23 9:55
* @Version 1.0
* @Author gezongyang
*/
@Component
public class ApplicationRunnerDemo implements ApplicationRunner {
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("====getOptionNames======"+args.getOptionNames());
        System.out.println("====getOptionValues====="+args.getOptionValues("key"));
    }
}

配置參數(shù)啟動(dòng)

打印結(jié)果:

====getOptionNames======[key]
====getOptionValues=====[value]

八、實(shí)現(xiàn)SmartLifecycle 接口

package org.springframework.context;
 
public interface SmartLifecycle extends Lifecycle, Phased {
    boolean isAutoStartup();
    void stop(Runnable var1);
}

SmartLifecycle 是 Spring Framework 中的一個(gè)接口,它擴(kuò)展了 Lifecycle 接口,并提供了更靈活的生命周期管理功能。使用 SmartLifecycle 可以更精細(xì)地控制應(yīng)用組件的啟動(dòng)和關(guān)閉過(guò)程。以下是 SmartLifecycle 的一些主要特性:

特性:

isAutoStartup(): 返回一個(gè)布爾值,指示該組件是否應(yīng)該在容器啟動(dòng)時(shí)自動(dòng)啟動(dòng)。

getPhase(): 返回一個(gè)整數(shù)值,表示組件的啟動(dòng)順序(階段)。較低的數(shù)字表示較早的啟動(dòng)順序。

start(): 啟動(dòng)組件,當(dāng)返回時(shí),組件應(yīng)該已經(jīng)啟動(dòng)完畢。

stop(Runnable callback): 停止組件,并且可以在停止完成后執(zhí)行提供的回調(diào)函數(shù)。

isRunning(): 返回一個(gè)布爾值,指示組件當(dāng)前是否處于運(yùn)行狀態(tài)。

案例:

package com.cfcc.teis.load;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
 
/**
 * @Description SmartLifecycle 可以更精細(xì)地控制應(yīng)用組件的啟動(dòng)和關(guān)閉過(guò)程
 * @date 2024/12/5 15:10
 * @Version 1.0
 * @Author gezongyang
 */
@Slf4j
@Component
public class SmartLifecycleDemo implements SmartLifecycle {
 
    private volatile boolean running = false;
 
    /**
     * SmartLifecycle run
     */
    @Override
    public void start() {
        log.info("SmartLifecycle run!");
        this.running = true;
    }
 
    @Override
    public void stop() {
 
    }
 
    /**
     * 在這里放置停止邏輯
     * @param callback
     */
    @Override
    public void stop(Runnable callback) {
        this.running = false;
        callback.run();  // 確保回調(diào)被執(zhí)行
    }
 
    @Override
    public boolean isRunning() {
        return this.running;
    }
 
    /**
     * 組件將在Spring上下文加載后自動(dòng)啟動(dòng)
     * @return
     */
    @Override
    public boolean isAutoStartup() {
        return true;
    }
 
    /**
     * 定義啟動(dòng)順序,數(shù)字越小越先啟動(dòng)
     * @return
     */
    @Override
    public int getPhase() {
        return 0;
    }
}

注意事項(xiàng)

當(dāng)你實(shí)現(xiàn) SmartLifecycle 時(shí),確保 start() 方法不會(huì)阻塞,因?yàn)樗鼤?huì)延遲整個(gè)應(yīng)用程序的啟動(dòng)。如果需要長(zhǎng)時(shí)間運(yùn)行的任務(wù),考慮將其放入單獨(dú)的線程中。

stop(Runnable callback) 方法中的回調(diào)是重要的,必須調(diào)用它來(lái)通知框架組件已經(jīng)停止。如果你不打算使用某些方法,可以不重寫(xiě)它們;默認(rèn)實(shí)現(xiàn)將提供合理的行為。

通過(guò)使用 SmartLifecycle,你可以更好地控制應(yīng)用程序中不同組件的生命周期行為,這對(duì)于那些對(duì)啟動(dòng)和關(guān)閉有特殊需求的應(yīng)用特別有用。

SmartLifecycle 的執(zhí)行時(shí)機(jī):

是在 Spring 應(yīng)用上下文刷新完成之后,即所有的 Bean 都已經(jīng)被實(shí)例化和初始化之后。具體來(lái)說(shuō),Spring 容器在調(diào)用 finishRefresh() 方法時(shí)會(huì)觸發(fā)所有實(shí)現(xiàn)了 SmartLifecycle 接口的 Bean 的啟動(dòng)過(guò)程。這是通過(guò)容器中的 LifecycleProcessor 來(lái)管理的,默認(rèn)使用的是 DefaultLifecycleProcessor。

SmartLifecycle 執(zhí)行的具體流程

上下文刷新完成后:當(dāng) Spring 應(yīng)用上下文完成刷新(finishRefresh),意味著所有的 Bean 已經(jīng)被加載并初始化完畢。

初始化生命周期處理器:Spring 會(huì)初始化 LifecycleProcessor,如果沒(méi)有顯式定義,則創(chuàng)建一個(gè)默認(rèn)的 DefaultLifecycleProcessor。

調(diào)用 onRefresh():接著,Spring 會(huì)調(diào)用 LifecycleProcessor 的 onRefresh() 方法,這將導(dǎo)致調(diào)用 startBeans() 方法。

根據(jù) phase 分組并排序:startBeans() 方法會(huì)獲取所有實(shí)現(xiàn)了 Lifecycle 接口的 Bean,并根據(jù)它們的 phase 屬性進(jìn)行分組和排序。phase 值越小的 Bean 將越早啟動(dòng)。

檢查 isAutoStartup():對(duì)于每個(gè) SmartLifecycle Bean,如果它的 isAutoStartup() 方法返回 true,那么它的 start() 方法就會(huì)被自動(dòng)調(diào)用。

執(zhí)行 start() 方法:按照 phase 排序后的順序,依次調(diào)用每個(gè) SmartLifecycle Bean 的 start() 方法來(lái)啟動(dòng)組件。

設(shè)置運(yùn)行狀態(tài):start() 方法應(yīng)該確保組件處于運(yùn)行狀態(tài),并且 isRunning() 方法應(yīng)返回 true。

同樣的邏輯也適用于停止組件的過(guò)程,但是是以相反的順序執(zhí)行的,即 phase 值較大的 Bean 會(huì)先停止。

因此,如果你希望在 Spring 容器完全準(zhǔn)備好之后執(zhí)行某些任務(wù),比如開(kāi)啟消息監(jiān)聽(tīng)、啟動(dòng)定時(shí)任務(wù)等,你可以實(shí)現(xiàn) SmartLifecycle 接口并在 start() 方法中放置這些邏輯。同時(shí),通過(guò)調(diào)整 getPhase() 返回的值,可以控制多個(gè) SmartLifecycle Bean 之間的啟動(dòng)順序。

以上就是SpringBoot初始化加載配置的八種方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot初始化加載配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java 數(shù)據(jù)結(jié)構(gòu)之時(shí)間復(fù)雜度與空間復(fù)雜度詳解

    Java 數(shù)據(jù)結(jié)構(gòu)之時(shí)間復(fù)雜度與空間復(fù)雜度詳解

    算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用: 時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小
    2021-11-11
  • SpringBoot文件上傳的原理解析

    SpringBoot文件上傳的原理解析

    這篇文章主要介紹了SpringBoot文件上傳的原理解析,SpringBoot 文件上傳是一種方便快捷的方式,可以將文件上傳到服務(wù)器,通過(guò)使用SpringBoot的文件上傳功能,可以輕松地實(shí)現(xiàn)文件上傳功能,需要的朋友可以參考下
    2023-10-10
  • JSqlParse完整介紹

    JSqlParse完整介紹

    JSqlParse是一款很精簡(jiǎn)的sql解析工具,本文主要介紹了JSqlParse完整介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Java面試必備之ArrayList陷阱解析

    Java面試必備之ArrayList陷阱解析

    昨天小楓接到了一個(gè)公司的面試電話,其中一道面試題覺(jué)得有點(diǎn)意思,在這里和大家一起分享下。面試題是ArrayList如何刪除指定元素。乍聽(tīng)很簡(jiǎn)單的問(wèn)題,但是如果沒(méi)有實(shí)際踩過(guò)坑很容易掉進(jìn)面試官的陷阱中,我們一起來(lái)分析下吧
    2022-02-02
  • Java反射的應(yīng)用之動(dòng)態(tài)代理深入理解

    Java反射的應(yīng)用之動(dòng)態(tài)代理深入理解

    這篇文章主要介紹了Java反射的應(yīng)用之動(dòng)態(tài)代理深入理解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 詳解JAVA 弱引用

    詳解JAVA 弱引用

    這篇文章主要介紹了 JAVA 弱引用的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)java引用對(duì)象,感興趣的朋友可以了解下
    2020-08-08
  • Java中Integer方法實(shí)例詳解

    Java中Integer方法實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Java中Integer方法的相關(guān)資料,Java中的Integer是int的包裝類型,文中通過(guò)代碼實(shí)例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Mybatis-Plus的saveOrUpdateBatch(null)問(wèn)題及解決

    Mybatis-Plus的saveOrUpdateBatch(null)問(wèn)題及解決

    這篇文章主要介紹了Mybatis-Plus的saveOrUpdateBatch(null)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java?泛型超詳細(xì)入門(mén)講解

    Java?泛型超詳細(xì)入門(mén)講解

    這篇文章主要介紹了Java基礎(chǔ)泛型詳情,泛型是JDK5中引入的特性,它提供了編譯時(shí)類型安全檢測(cè)機(jī)制,該機(jī)制允許在編譯時(shí)檢測(cè)到非法的類型,下面文章的詳細(xì)介紹,需要的朋友可以參考一下
    2022-04-04
  • MyBatis-Plus中如何使用ResultMap的方法示例

    MyBatis-Plus中如何使用ResultMap的方法示例

    本文主要介紹了MyBatis-Plus中如何使用ResultMap,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評(píng)論