首页 > 解决方案 > 有不止一个 b​​ean - 自定义注释突出显示错误

问题描述

我们创建了自定义 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我们确实需要正则表达式避免列出所有这些:qaprod
@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)$") 就可以了

标签: springspring-bootspring-annotations

解决方案


推荐阅读