Vin*_*ino 28 java spring amazon-dynamodb spring-bean spring-boot
我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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>
{
}
Run Code Online (Sandbox Code Playgroud)
AgentController.java(使用AgentRepository的地方)
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring建议如下:
> 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这里的意思是什么?是因为我的应用程序配置有问题吗?它怎么可能已经注册?
请给我一些指示,因为我对我的下一步感到困惑.
Dar*_*the 61
自Spring Boot 2.1以来必须启用Bean重写,
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes
Bean Overriding
默认情况下禁用Bean覆盖以防止意外覆盖bean.如果依赖于覆盖,则需要将spring.main.allow-bean-definition-overriding设置为true.
组
spring.main.allow-bean-definition-overriding=true
Run Code Online (Sandbox Code Playgroud)
或者是yml,
spring:
main:
allow-bean-definition-overriding: true
Run Code Online (Sandbox Code Playgroud)
再次启用覆盖.
She*_*ott 16
例如,使用这种方法启用 bean 覆盖
@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
Run Code Online (Sandbox Code Playgroud)
或者
@SpringBootApplication (properties = "spring.main.allow-bean-definition-overriding=true")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23118 次 |
| 最近记录: |