春季-多个配置文件处于活动状态 [英] Spring - Multiple Profiles active

查看:80
本文介绍了春季-多个配置文件处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上在Spring中有一个bean,仅在激活2个配置文件时才想激活它.基本上是这样的:

I basically have a bean in Spring that I wanted to activate only when 2 profiles are active. Basically, it would be like:

@Profile({"Tomcat", "Linux"})
public class AppConfigMongodbLinux{...}

@Profile({"Tomcat", "WindowsLocal"})
public class AppConfigMongodbWindowsLocal{...}

因此,我想在使用-Dspring.profiles.active=Tomcat,WindowsLocal时尝试仅使用AppConfigMongodbWindowsLocal,但仍尝试注册AppConfigMongodbLinux.

So I'd like that when I use -Dspring.profiles.active=Tomcat,WindowsLocal, it would try to use only the AppConfigMongodbWindowsLocal, but it still tries to register the AppConfigMongodbLinux.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfigMongodbLinux': Injection of autowired dependencies failed

是否可以仅在两个概要文件都处于活动状态或我使用不正确时才注册Bean? :)

Is it possible to make the bean be registerd only when both profiles are active or am I using it incorrectly? :)

谢谢!

发布整个堆栈.

错误实际上是在属性上缺少的属性上的,但是此bean是否会被激活?我想了解这一点,以确保不会激活错误的bean.

The error is actually on a property that is missing on the properties, but will this bean get activated? I wanted to understand this to ensure I'm not activating a wrong bean..

org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfigMongodbLinux': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Integer mycompany.config.AppConfigMongodbLinux.mongoPort; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'mongo.port' in string value "${mongo.port}"
    ... 40 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Integer mycompany.config.AppConfigMongodbLinux.mongoPort; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'mongo.port' in string value "${mongo.port}"
    ...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mongo.port' in string value "${mongo.port}"

推荐答案

不幸的是,如果列出的 any 配置文件处于活动状态,则@Profile会激活.有两种解决方法.

Unfortunately, @Profile activates if any listed profile is active. There are a couple of ways around this.

  • 将通用@Profile("Tomcat")注释应用于顶级配置类,然后将@Profile("Windows")应用于嵌套配置类(或@Bean方法).
  • 如果可以接受Spring Boot作为依赖项,请使用
  • Apply the common @Profile("Tomcat") annotation to a top-level configuration class, and then apply @Profile("Windows") to a nested configuration class (or @Bean method).
  • If Spring Boot is acceptable as a dependency, use @AllNestedConditions to create an annotation that's the AND instead of the OR.

如果您使用的是Spring Boot自动配置类,那么您尝试做的事情看起来很干净.如果在您应用程序生命周期的这个阶段引入自动配置是可行的,那么我建议您考虑使用它.

It looks like what you're trying to do would be clean to write if you were using Spring Boot autoconfiguration classes; if it's practical to introduce autoconfiguration at this stage of your application's lifecycle, I recommend considering it.

这篇关于春季-多个配置文件处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆