簡體   English   中英

Spring 啟動:@Profile @Bean 組合不起作用

[英]Spring boot: @Profile @Bean combination doesn't work

我已經對這兩個 bean 進行了編碼:

@Bean
public HttpClient httpClient() throws Exception {
    LOG.debug("http client for NO PRE");

    return HttpClients.custom().build();
}

@Bean
@Profile("pre")
public HttpClient httpClientPre() throws Exception {
    LOG.debug("http client for PRE");

    //...

    HttpClient client = HttpClients.custom().build();

    return client;
}

在另一邊,我有另一個豆子:

@Bean
@Primary
public RestTemplate restTemplate(RestTemplateBuilder builder, HttpClient httpClient) throws Exception {
    return builder.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient))
            .build();
}

如您所知,當"pre"處於活動狀態時,我希望達到httpClientPre 但是,盡管活動配置文件是“pre”,但仍未達到。 查看日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

13:20:31.746 [main] INFO  n.g.t.e.s.SchedulerApplication - Starting SchedulerApplication on psgd with PID 9538 (/home/jeusdi/projects/repositori-digital/rep-digital-scheduler/target/classes started by jeusdi in /home/jeusdi/projects/repositori-digital)
13:20:31.760 [main] DEBUG n.g.t.e.s.SchedulerApplication - Running with Spring Boot v2.0.4.RELEASE, Spring v5.0.8.RELEASE
13:20:31.767 [main] INFO  n.g.t.e.s.SchedulerApplication - The following profiles are active: pre  <<<<<<<<<<<<

但是,我期待獲得"http client for PRE"日志。 盡管如此,我得到:

13:20:48.613 [main] DEBUG n.g.t.e.s.c.ServicesConfiguration - http client for NO PRE <<<<<<

這意味着盡管當前配置文件是pre ,但仍未達到httpClientPre

有任何想法嗎?

編輯

我也試過@Profile("!pre") ,但我收到了這條消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method restTemplate in net.gencat.transversal.espaidoc.scheduler.config.ServicesConfiguration required a bean named 'httpClient' that could not be found.


Action:

Consider defining a bean named 'httpClient' in your configuration.

編輯2

我也嘗試過:

在此處輸入圖像描述

但它不斷收到上面的消息。

看來你應該用@Profile標記所有@Bean方法

根據https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.ZFC35FDC70D5FC69D7A2D5698ZA822

注意:在@Bean 方法上使用@Profile,可能會出現一種特殊情況:在重載@Bean 方法的情況下,相同的Java 方法名稱(類似於構造函數重載),需要在所有重載方法上一致地聲明@Profile 條件. 如果條件不一致,則只有重載方法中第一個聲明的條件才重要。 因此,@Profile 不能用於 select 一個具有特定參數簽名的重載方法; 同一 bean 的所有工廠方法之間的解析在創建時遵循 Spring 的構造函數解析算法。 如果您想定義具有不同配置文件條件的替代 bean,請使用指向相同 bean 名稱的不同 Java 方法名稱; 請參閱 @Configuration 的 javadoc 中的 ProfileDatabaseConfig。

這里的原因是您正在創建具有 2 個不同名稱的 bean(方法名稱 == bean 的名稱),並且在發生注入時會考慮 bean 的名稱 - bean 名稱 == 參數名稱。

在您的情況下,您正在注入httpClient但您正在創建httpClienthttpClientPre - 因此注入了httpClient

使用@Profile("!pre")是通往 go 但與@Qualifier結合使用的方式,因此您可以正確命名 bean,例如。

@Bean
@Profile("!pre")
public HttpClient httpClient() throws Exception 

@Bean
@Profile("pre")
@Qualifier("httpClient")
public HttpClient httpClientPre() throws Exception 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM