spring-boot: Configuration Processor @Bean Duplicate Prefix Definition APT Error

When using the spring-boot-configuration-processor and leveraging the same @ConfigurationProperties prefix on an @Bean I am seeing an APT error in Eclipse in 1.4.0.RELEASE. While I agree that this error should be generated when using a duplicate prefix on an @Component class definition, I think this restriction should be relaxed on @Bean definitions.

I am referencing the External Config 3rd Party Configuration section in the reference guide.

Basic example of the issue

@Bean
@ConfigurationProperties("spring.datasource.tomcat")
public DataSource readDataSource() {
        return DataSourceBuilder.create()
            .url(properties.getReadUrl())
            .username(properties.getUsername())
            .password(properties.getPassword())
            .build();
}

@Bean
@ConfigurationProperties("spring.datasource.tomcat")
public DataSource readWriteDataSource() {
        return DataSourceBuilder.create()
            .url(properties.getReadUrl())
            .username(properties.getUsername())
            .password(properties.getPassword())
            .build();
}
Duplicate `@ConfigurationProperties` definition for prefix 'spring.datasource.tomcat'.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 2
  • Comments: 15 (8 by maintainers)

Most upvoted comments

我觉得楼主说的挺对的,@ConfigurationProperties注解本身的作用就是将特定的配置属性应用到某个springbean中,从这一点来讲,此注解本身不应该限定其prefix是否重复。 我当前遇到的问题就是,当我使用springboot配置多数据源时,我需要将hikari的相关配合应用到所有的数据源上,而且我希望所有数据源共享这些配置,因此我在每个数据源上都使用了@ConfigurationProperties("spring.datasource.hikari")这样的注解,然而springboot确提示我重复使用了@ConfigurationProperties,即使我将这两个注解写在了不同的类中 希望springboot官方能看到此issue,并修正此问题

Courtesy of Google translate

I think the landlord is quite right. The @ConfigurationProperties annotation itself is used to apply specific configuration properties to a springbean. From this point of view, the annotation itself should not limit whether its prefix is duplicated. The problem I am currently experiencing is that when I use springboot to configure multiple data sources, I need to apply hikari’s related cooperation to all data sources, and I want all data sources to share these configurations, so I am at each data source. Both use the annotation @ConfigurationProperties(“spring.datasource.hikari”), but springboot does prompt me to reuse @ConfigurationProperties, even though I wrote these two annotations in different classes. I hope springboot official can see this issue and fix this problem.

@sxyandapp A meta-data entry is related to a group and a group is related to a source. If we’d allowed this, you’d get the same keys duplicated over and over again (which matches your situation). You want the exact opposite (a single key that configures multiple groups). I’d rather have dedicated namespace (so that you can fine tune thing individually) and an EnvironmentPostProcessor that can map a “main namespace”

@wuwen5, I’d combine the two @Bean methods into one and use the Environment to check the value of the spring.datasource.hikari.refresh property and then set the Hikari system property as needed. If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.