春季启动:@Profile @Bean组合不起作用

时间:2019-10-10 11:29:24

标签: java spring-boot

我已经编码了这两个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:

@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 <<<<<<

这意味着尽管当前配置文件为httpClientPre,仍未达到pre

有什么想法吗?

编辑

我也尝试使用@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.

EDIT2

我也尝试过:

enter image description here

但是它总是在上面得到消息。

2 个答案:

答案 0 :(得分:0)

似乎您应该用@Bean

标记所有 @Profile个方法

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

  

注意:在@Bean方法上使用@Profile时,可能会出现特殊情况:在具有相同Java方法名称的@Bean方法重载的情况下(类似于构造函数重载),需要在@Profile条件上一致声明@Profile条件所有重载的方法。如果条件不一致,则只有重载方法中第一个声明的条件才重要。因此,@ Profile不能用于选择具有特定参数签名的重载方法;同一bean的所有工厂方法之间的解析在创建时遵循Spring的构造函数解析算法。如果要定义具有不同概要文件条件的备用Bean,请使用指向相同Bean名称的不同Java方法名称。参见@Configuration的javadoc中的ProfileDatabaseConfig。

答案 1 :(得分:0)

这里的原因是您要创建具有2个不同名称(方法名称== bean名称)的bean,并且在注入发生时会考虑bean的名称-bean name ==参数名称。

在您的情况下,您正在注入httpClient,但是您正在创建httpClienthttpClientPre-这样就注入了httpClient

使用@Profile("!pre")是一种方法,但要与@Qualifier结合使用,这样您就可以正确地命名bean,例如。

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

@Bean
@Profile("pre")
@Qualifier("httpClient")
public HttpClient httpClientPre() throws Exception 
相关问题