在两个不同的配置文件中重复使用了 `@ConfigurationProperties`。

huangapple go评论57阅读模式
英文:

Duplicate `@ConfigurationProperties` in two different profile

问题

为什么我不能定义两个具有相同configurationProperties前缀的bean?以下代码引发以下错误:

为前缀'config'重复的`@ConfigurationProperties`定义

我知道这是为了两个不同的配置文件。我是否有其他方法可以做到?

@Profile("MyProfile")
@Bean(initMethod = "init", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public MyConfig config() {
    MyConfig config = new MyConfig();
    return config;
}

@Profile("!MyProfile")
@Bean(initMethod = "anotherInit", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public MyConfig myConfig() {
    MyConfig config = new MyConfig();
    return config;
}

更新:

[错误] 无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) 在项目my-app上:编译失败:
编译失败:

[错误] ...MyConfig.java:[21,22] 为前缀'config'重复的@ConfigurationProperties定义

英文:

Why can't I define two bean with the same configurationProperties prefix? Code below throws the following error:

Duplicate `@ConfigurationProperties` definition for prefix 'config'

I know it is the same but for two different profile. Is there any other way I may
do it ?

@Profile("MyProfile")
@Bean(initMethod = "init", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public MyConfig config() {
    MyConfig config = new MyConfig();
    return config;
}

@Profile("!MyProfile")
@Bean(initMethod = "anotherInit", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public MyConfig myConfig() {
    MyConfig config = new MyConfig();
    return config;
}

UPDATE:

> [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project my-app: Compilation failure:
Compilation failure:

> [ERROR] ...MyConfig.java:[21,22] Duplicate @ConfigurationProperties definition for prefix 'config'

答案1

得分: 2

在您的评论中提到,这个编译时错误是由注解处理器 - spring-boot-configuration-processor 引入的。

它是如何确定是否存在重复的配置属性?

它根据两个因素来确定 -

  1. 属性前缀
  2. 配置类名称 / 方法的返回类型

请注意,Spring配置文件名称不是查找重复项的一部分。因此,这两个配置是相似的。这导致了编译时错误。

解决方法

要么您向Spring API提出错误报告,要么采取一些措施使这两个属性不同。我认为唯一的方法是创建一个新的配置类,它扩展了原始配置类。这将确保它们不被视为重复项。

class SomeOtherProfileConfig extends MyConfig {
}

@Profile("MyProfile")
@Bean(initMethod = "init", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public MyConfig config() {
    MyConfig config = new MyConfig();
    return config;
}

@Profile("!MyProfile")
@Bean(initMethod = "anotherInit", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public SomeOtherProfileConfig myConfig() {
    SomeOtherProfileConfig config = new SomeOtherProfileConfig();
    return config;
}
英文:

Like mentioned in your comment, this compile time error is introduced by annotation processor - spring-boot-configuration-processor.

How it decides whether duplicate configuration properties exists ?

It decides on 2 things -

  1. Property Prefix
  2. Configuration class name / Return Type of the method

Notice that Spring profile name isn't part of finding duplicates. So, both configurations are similar. This is causing the compile time error.

Solution

Either you raise a bug with the spring api or do something that makes those 2 properties different. One way and I think the only way is to create a new configuration class that extends original one. This will ensure that they are not treated as duplicates.

class SomeOtherProfileConfig extends MyConfig {
}

@Profile("MyProfile")
@Bean(initMethod = "init", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public MyConfig config() {
    MyConfig config = new MyConfig();
    return config;
}

@Profile("!MyProfile")
@Bean(initMethod = "anotherInit", name = "MyConfig")
@ConfigurationProperties(prefix = "config")
public SomeOtherProfileConfig myConfig() {
    SomeOtherProfileConfig config = new SomeOtherProfileConfig();
    return config;
}

答案2

得分: 0

你可以为 @ConfigurationProperties 声明一个单独的类,属性将从活动配置文件中获取。然后,你可以使用 @EnableConfigurationProperties 在不同的 Bean 中使用这些属性。

英文:

You can declare a single class for the @ConfigurationProperties, the properties will be picked from the active profile's properties file. Then, you can use the properties in different beans with @EnableConfigurationProperties

huangapple
  • 本文由 发表于 2020年9月7日 20:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63777266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定