SpringBoot - BeanDefinitionOverrideException:无效的Bean定义

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

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'agentRepository' defined in null: Cannot register bean definition [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; destroyMethodName=null] for bean 'agentRepository': There is already [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; destroyMethodName=null] bound.

我已经广泛搜索了SO和互联网,但没有任何有用的解决方案。错误消息也具有误导性。

我的项目属于以下层次结构

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

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);
    }
}

代理存储库.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>
{
}

代理控制器.java(使用代理存储库的位置)

@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);
    }
}

春天建议如下: .> The bean 'agentRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled

这是什么意思?是因为我的应用程序配置中出现问题吗?另外,它怎么可能已经注册了?null

请给我一些指示,因为我对下一步感到非常困惑。


答案 1

自Spring Boot 2.1以来,必须启用Bean覆盖,

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes

豆覆盖

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

设置

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

或 yml,

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

以再次启用覆盖。

编辑

Bean Overovering 基于 Bean 的名称,而不是其类型。例如:

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

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

在 > 2.1 中将导致此错误,默认情况下,Bean 名称取自方法名称。重命名方法或将属性添加到批注将是一个有效的修复。nameBean


答案 2

例如,使用这种方法启用Bean覆盖

@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")

@SpringBootApplication (properties = "spring.main.allow-bean-definition-overriding=true")

推荐