具有该名称的 bean 已在类路径资源 [path] 中定义,并且覆盖已禁用

问题描述 投票:0回答:4

我有 Spring Data Elaticsearch(使用 Transport Client)和 ESTemplate 的 java 配置。 这里有一些例外:

@Configuration
@EnableElasticsearchRepositories(basePackages = "subpackage-in-this-project")
@PropertySource("file:path-to-file")
public class ESConfig {

    @Bean
    ElasticsearchTemplate elasticsearchTemplate(Client client) {
        return new ElasticsearchTemplate(client);
    }

    @Bean
    Client client() { 
// configuration of the ES client
   }

}

我有一个配置可以在不同的项目中扩展上面的配置。

@Configuration
@ComponentScan("package-prefix-that-matches-packages-in-both-projects")
@EnableElasticsearchRepositories(basePackages = "subpackage-in-this-project")
@PropertySource("file:same-path-to-file-as-in-the-config-above")
public class ExtendedESConfig extends ESConfig {

    @Value("index-name")
    private String indexName;

    @Bean
    public String indexName() {
        return indexName;
    }
}

在执行第三个 Spring Boot 应用程序时,该应用程序使用

ExtendedESConfig
对项目的依赖,我得到了这个,但我不太明白为什么会发生这种情况,从 2.0 升级到 2.2.9.RELEASE 后开始发生。 5.发布Spring Boot版本。


***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'elasticsearchTemplate', defined in class path resource [my/package/ESConfig.class], could not be registered. A bean with that name has already been defined in class path resource [my/other/package/ExtendedESConfig.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

2020-08-30 16:49:46 ERROR [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:40 - 

我的评论中的重要评论:

...可悲的是,我不是编写此 ES 配置并围绕它构建整个基础设施的人。 ...

在提出这个问题时,我不拥有 ExtendedESConfig,也无法更改它。

spring.main.allow-bean-definition-overriding=true
不是答案,因为我不想覆盖 bean 声明。

spring-boot exception initialization spring-data-elasticsearch spring-bean
4个回答
17
投票

或者您可以将下一个属性添加到您的

application.properties
:

spring.main.allow-bean-definition-overriding=true

5
投票

覆盖 bean 的默认行为已在 Spring Boot 2.1 中禁用。 Spring Boot 2.1 发行说明

因为您不拥有/或不想修改这两个配置类。您可以使用

SpringBootApplication
@ComponentScan

类中排除父配置
@SpringBootApplication
@ComponentScan(excludeFilters = 
        {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ESConfig.class)})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

0
投票

在我的

springSecurityFilterChain
类中,我的自定义
@Configuration
方法也遇到了类似的问题。应用程序告诉我它无法创建名为
springSecurityFilterChain
的 Bean,因为它是在其他地方定义的(在我的例子中,在 Spring Security 包中,与您的情况一样,我无法修改)。

我找到了解决方案here,它相当于简单地更改我的自定义方法的名称;我选择了

customFilterChain
。所以它从

@Bean
public SecurityFilterChain springSecurityFilterChain(HttpSecurity http) throws Exception {
    return http
            .csrf().disable()
            // etc
}

至:

@Bean
public SecurityFilterChain customFilterChain(HttpSecurity http) throws Exception {
    return http
            .csrf().disable()
            // etc
}

令人惊讶的是它有效。正如文章所述,Spring 默认使用方法名称构建 bean。希望有帮助。


-3
投票

在你的模块中找到:resources/application.properties 并写入:

spring.main.allow-bean-definition-overriding=true

它对你有帮助,你需要启用bean覆盖机制。

© www.soinside.com 2019 - 2024. All rights reserved.