问题描述
我正在尝试使用 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:无效的 bean 定义,名称为 'agentRepository' 在 null 中定义:无法注册 bean 定义 [根 bean:类 [org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactoryBean];范围=;摘要=假;懒惰初始化=假;自动线模式=0;依赖检查=0;自动接线候选=真;主要=假;工厂BeanName=空;工厂方法名=空;初始化方法名=空;bean 'agentRepository' 的 destroyMethodName=null]:已经有 [根 bean:类 [org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactoryBean];范围=;摘要=假;懒惰初始化=假;自动线模式=0;依赖检查=0;自动接线候选=真;主要=假;工厂BeanName=空;工厂方法名=空;初始化方法名=空;destroyMethodName=null] 绑定.
我已经广泛搜索了 SO 和互联网,但没有任何有用的解决方案.错误消息也具有误导性.
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);
}
}
Spring 建议如下:<代码>>在 null 中定义的 bean 'agentRepository' 无法注册.已在 null 中定义了具有该名称的 bean,并且已禁用覆盖.
Spring suggests the following:
> 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
在这里是什么意思?是因为我的应用程序配置有问题吗?还有怎么可能已经注册了?
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-Release-Notes
Bean 覆盖
默认情况下禁用 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.
设置
spring.main.allow-bean-definition-overriding=true
或yml,
spring:
main:
allow-bean-definition-overriding: true
再次启用覆盖.
编辑,
Bean Overriding 是基于 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定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!