SpringBoot-BeanDefinitionOverrideException:无效的Bean定义 [英] SpringBoot - BeanDefinitionOverrideException: Invalid bean definition

查看:1578
本文介绍了SpringBoot-BeanDefinitionOverrideException:无效的Bean定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Boot在本地设置DynamoDB。最初,我开始进行设置,并且能够通过存储库将其写入/保存到DynamoDB。从那时起,我添加了更多类来构建我的应用程序。现在,当我尝试启动应用程序时,出现以下异常:

I am trying to setup DynamoDB locally with Spring Boot. Initially I got the setup working and was able to write/save to DynamoDB via a repository. From that point I added more classes to build my application. Now when I try to start my application, I get the following exception:


org.springframework.beans.factory .support.BeanDefinitionOverrideException:名称为'agentRepository'的无效bean定义(在null中定义):无法注册bean定义[root bean:class [org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactoryBean]; scope =; abstract = false; lazyInit = false; autowireMode = 0; dependencyCheck = 0; autowireCandidate = true; primary = false; factoryBeanName = null; factoryMethodName = null; initMethodName = null; Bean'agentRepository'的destroyMethodName = null]:已经存在[Root bean:类[org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactoryBean]]; scope =; abstract = false; lazyInit = false; autowireMode = 0; dependencyCheck = 0; autowireCandidate = true; primary = false; factoryBeanName = null; factoryMethodName = null; initMethodName = null;

我已经广泛搜索了SO和Internet,但是没有任何有用的解决方案为此。错误消息也会引起误解。

I have searched SO and internet extensively but there were no any useful solution to this. The error message is misleading as well.

我的项目具有以下层次结构

My project is of the following hierarchy

ai.test.as
  - as
      - agent
          - business
          - intent
          - exception
          - Agent.java
          - AgentDTO.java
          - AgentRespository.java
          - AgentController.java
          - AgentService.java
          - AgentServiceImpl.java
  - config
     - DynamoDBConfig.java

DynamoDBConfig.java

DynamoDBConfig.java

package ai.test.as.config;

import ai.test.as.agent.AgentRepository;
import ai.test.as.agent.intent.template.TemplateRepository;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableDynamoDBRepositories(basePackageClasses = {AgentRepository.class})
public class DynamoDBConfig
{
    @Value("${aws.dynamodb.endpoint}")
    private String dynamoDBEndpoint;

    @Value("${aws.auth.accesskey}")
    private String awsAccessKey;

    @Value("${aws.auth.secretkey}")
    private String awsSecretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB()
    {
        AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(getAwsCredentials());
        dynamoDB.setEndpoint(dynamoDBEndpoint);

        return dynamoDB;
    }

    @Bean
    public AWSCredentials getAwsCredentials()
    {
        return new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    }
}

AgentRepository.java

AgentRepository.java

package ai.test.as.agent;

import ai.test.as.agent.Agent;
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;

@EnableScan
public interface AgentRepository extends CrudRepository<Agent, String>
{
}

AgentController.java(使用AgentRepository的地方)

AgentController.java (Where AgentRepository is used)

@RestController
@RequestMapping(value = "/v1/agents")
public class AgentController
{
    @Autowired
    private AgentRepository agentRepository;

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public void test()
    {
        Agent agent = new Agent();
        agent.setAgentNumber("123456");
        agent.setId(1);

        agentRepository.save(agent);
    }
}

春季建议以下内容:
>无法注册以null定义的bean agentRepository。具有该名称的bean已被定义为null,并且禁止覆盖

null 是这里吗?是因为我的应用程序配置有问题吗?

What does null mean here? Is it because something wrong in my application config? Also how is it possible that it is already registered?

请给我一些提示,因为我对下一步工作感到困惑。

Please give me some pointers because I so confused about my next steps.

推荐答案

自Spring Boot 2.1起必须启用Bean覆盖

Bean overriding has to be enabled since Spring Boot 2.1,

https://github.com/spring-projects/spring-boot/wiki/Spring- Boot-2.1-发行说明


Bean覆盖

Bean Overriding

默认情况下禁用Bean覆盖,以防止意外覆盖Bean。如果您要依赖覆盖,则需要将spring.main.allow-bean-definition-overriding设置为true。

Bean overriding has been disabled by default to prevent a bean being accidentally overridden. If you are relying on overriding, you will need to set spring.main.allow-bean-definition-overriding to true.

Set

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



或yml,

or yml,

spring:
   main:
     allow-bean-definition-overriding: true

再次启用覆盖。

编辑,

Bean覆盖基于Bean的名称而不是其类型。例如

Bean Overriding is based of the name of the bean not its type. e.g.

@Bean
public ClassA class(){
   return new ClassA();
}

@Bean
public ClassB class(){
   return new ClassB();
}

会导致此错误> 2.1,默认情况下,bean名称取自方法名称。重命名方法或将 name 属性添加到 Bean 批注将是有效的解决方法。

Will cause this error in > 2.1, by default bean names are taken from the method name. Renaming the method or adding the name attribute to the Bean annotation will be a valid fix.

这篇关于SpringBoot-BeanDefinitionOverrideException:无效的Bean定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆