spring - 有不止一个 bean - 自定义注释突出显示错误
问题描述
我们创建了自定义 spring 注释@ProfileRegex
来升级简单的@Profile
功能
这是一个用法示例:
@Configuration
public class MySomeConfigClass {
@Bean
@ProfileRegex("^(test|dev)$")
public OurCustomInterface getLocalImplementation() {
return new LocalImplementationOfOurCustomInterface();
}
@Bean
@ProfileRegex("^(qa|prod)$")
public OurCustomInterface getCloudImplementation() {
return new CloudImplementationOfOurCustomInterface();
}
@Bean
public void CustomClassTakesOurInterfaceAsArg getSomething(OurCustomInterface ourCustomInterface) {
return new CustomClassTakesOurInterfaceAsArg(ourCustomInterface);
}
}
在此示例中,IntellijourCustomInterface
在 3d 方法中突出显示信息错误:
Could not autowire. There is more than one bean of 'OurCustomInterface' type
如果我将该示例更改为简单@Profile
,它将理解这两个 bean 之间存在差异并且不显示错误,但是简单@Profile
对我们来说还不够,因为我们的配置文件比 , , 更复杂test
,而且dev
我们确实需要正则表达式避免列出所有这些:qa
prod
@Profile({"profile1", "profile2", "profile3", ***, "profileX"})
那么问题来了,如何让它像对待我们@ProfileRegex
一样简单@Profile
,不再高亮错误呢?
我确实添加@Primary
了其中一个,它修复了错误,但我们的团队认为这不是很好的方法,因为确实没有主要或次要的,因为所有这些都取决于配置文件条件。
PS不管IDEA中的错误,代码编译良好并根据需要工作,我只是不喜欢IDEA中的错误突出显示
任何提示?提前致谢
更新(添加我们的实现@ProfileRegex
):
@despadina 根据这篇文章https://raymondhlee.wordpress.com/2015/05/31/using-spring-4-condition-to-control-bean-registration/创建了一个要点。
只是我们的版本支持多个正则表达式,这是唯一的区别:https ://gist.github.com/dpomaresp/1cbfdd13e2985b5796e30ee1714d8785
因此,只需在类或方法级别添加 @ProfileRegex("^(test|dev)$") 就可以了
解决方案
推荐阅读
- java - 通过反射将值设置为java类?
- c++ - 您将如何使用标准引号解释指向基类和派生类成员的指针的这种差异?
- bash - Anaconda 环境 bash 前缀太长
- python - 查找日志文件中的模式计数驻留在 zip 文件中而不提取
- sql - 如何使用 SQL 找到每个组的最小值和最大值?
- php - ES256 JWT 在 PHP 中为 Apple AppStoreConenct API 身份验证签名
- java - 了解 JBoss EAP 6.4 (HornetQ) 中的远程 JMS 连接
- sql-server - 在 Linux 中添加 SSL 证书后 SQL Server 服务中断
- mongodb - 如何检查文档中是否存在元素并根据它返回true或false?
- r - R ggplotly重复的传说