問題描述
我正在嘗試使用 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定義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!