【发布时间】:2023-03-26 17:59:01
【问题描述】:
我基本上在 Spring 中有一个 bean,我只想在 2 个配置文件处于活动状态时激活它。基本上,它会是这样的:
@Profile({"Tomcat", "Linux"})
public class AppConfigMongodbLinux{...}
@Profile({"Tomcat", "WindowsLocal"})
public class AppConfigMongodbWindowsLocal{...}
所以我希望当我使用-Dspring.profiles.active=Tomcat,WindowsLocal 时,它会尝试仅使用AppConfigMongodbWindowsLocal,但它仍会尝试注册AppConfigMongodbLinux。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfigMongodbLinux': Injection of autowired dependencies failed
是否可以仅在两个配置文件都处于活动状态或我使用不正确时才注册 bean? :)
谢谢!!
编辑:发布完整堆栈。
错误实际上是在属性上缺少的属性上,但是这个 bean 会被激活吗?我想了解这一点,以确保我没有激活错误的 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}"
【问题讨论】:
-
这看起来不像是配置文件的问题。检查 appConfigMongodbLinux 中的自动装配 bean 是否可用。发布完整的堆栈跟踪。
-
@ShankarPS 看起来配置文件确实有问题。他不想激活的配置文件正在寻找该配置中不可用的属性。