简体   繁体   English

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

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

I've coded these two beans:我已经对这两个 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;
}

By other side, I've this another bean:在另一边,我有另一个豆子:

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

As you can figure out, when "pre" is active I want httpClientPre is reached.如您所知,当"pre"处于活动状态时,我希望达到httpClientPre However, in spite of active profile is "pre", it's not reached.但是,尽管活动配置文件是“pre”,但仍未达到。 See logs:查看日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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  <<<<<<<<<<<<

However, I was expecting to get log for "http client for PRE" .但是,我期待获得"http client for PRE"日志。 Nevertheless, I'm getting:尽管如此,我得到:

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

It means that httpClientPre is not reached in spite of current profile is pre .这意味着尽管当前配置文件是pre ,但仍未达到httpClientPre

Any ideas?有任何想法吗?

EDIT编辑

I've also tried with @Profile("!pre") , but I'm getting this message:我也试过@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编辑2

I've also tried with:我也尝试过:

在此处输入图像描述

But it keep getting message above.但它不断收到上面的消息。

It seems you should mark all @Bean methods with @Profile看来你应该用@Profile标记所有@Bean方法

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

NOTE: With @Profile on @Bean methods, a special scenario may apply: In the case of overloaded @Bean methods of the same Java method name (analogous to constructor overloading), an @Profile condition needs to be consistently declared on all overloaded methods.注意:在@Bean 方法上使用@Profile,可能会出现一种特殊情况:在重载@Bean 方法的情况下,相同的Java 方法名称(类似于构造函数重载),需要在所有重载方法上一致地声明@Profile 条件. If the conditions are inconsistent, only the condition on the first declaration among the overloaded methods will matter.如果条件不一致,则只有重载方法中第一个声明的条件才重要。 @Profile can therefore not be used to select an overloaded method with a particular argument signature over another;因此,@Profile 不能用于 select 一个具有特定参数签名的重载方法; resolution between all factory methods for the same bean follows Spring's constructor resolution algorithm at creation time.同一 bean 的所有工厂方法之间的解析在创建时遵循 Spring 的构造函数解析算法。 Use distinct Java method names pointing to the same bean name if you'd like to define alternative beans with different profile conditions;如果您想定义具有不同配置文件条件的替代 bean,请使用指向相同 bean 名称的不同 Java 方法名称; see ProfileDatabaseConfig in @Configuration's javadoc.请参阅 @Configuration 的 javadoc 中的 ProfileDatabaseConfig。

The reason here is that you are creating beans with 2 different names (method name == name of bean) and name of bean is taken into account when injection happens - there bean name == argument name.这里的原因是您正在创建具有 2 个不同名称的 bean(方法名称 == bean 的名称),并且在发生注入时会考虑 bean 的名称 - bean 名称 == 参数名称。

In your case you are injecting httpClient but you are creating httpClient and httpClientPre - thus httpClient is injected在您的情况下,您正在注入httpClient但您正在创建httpClienthttpClientPre - 因此注入了httpClient

using @Profile("!pre") is the way to go but in conjunction with @Qualifier so you can name the bean correctly, eg.使用@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.

相关问题 Spring Boot —为什么@ComponentScan无法与我的软件包组合一起使用? - Spring Boot — why doesn't the @ComponentScan work with my package combination? Spring 引导配置文件属性不适用于测试 - Spring Boot profile properties doesn't work with tests 自Spring Boot Upgrade以来,与@ConditionalOnMissingBean结合使用Define Bean不起作用 - Define Bean in combination with @ConditionalOnMissingBean does not work since Spring Boot Upgrade Spring Bean设置不起作用,没有这样的bean - Spring Bean setting doesn't work, no such bean Spring Boot @ConditionalOnProperty 不加载 bean - Spring Boot @ConditionalOnProperty doesn't load bean 使用自定义xml bean设置属性后,Spring Boot REST Controller不起作用 - Spring Boot REST Controller doesn't work after setting attribute with custom xml bean Spring bean() 切入点不适用于 OR 语句 - Spring bean() pointcut doesn't work with OR statement 使用Spring和Hibernate进行Bean验证不起作用 - Bean validation with Spring and Hibernate doesn't work Spring Profile无法与Aspect注释一起使用 - Spring Profile doesn't work with Aspect annotation Spring 引导:特定于 bean 创建配置文件 - Spring boot : bean creation profile specific
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM