我正在尝试使用 SpringBoot 设置本地 DynamoDB 实例。我正在关注this https://www.baeldung.com/spring-data-dynamodb但使用 Gradle。
当我尝试运行我的应用程序时,出现以下异常:
BeanCreationException: Error creating bean with name 'agentRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
我确实明白这是由于含糊不清而导致依赖注入失败,但我的AgentRepository
作为无参构造函数。不知道哪里有歧义。
以下是我的代码:
摇篮文件
buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'test.project'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}
ext['springCloudVersion'] = 'Greenwich.M3'
dependencies {
//implementation('org.springframework.boot:spring-boot-starter-data-redis')
implementation('org.springframework.boot:spring-boot-starter-web')
compile 'org.springframework.data:spring-data-releasetrain:Hopper-SR10'
compile 'com.amazonaws:aws-java-sdk-dynamodb:1.11.34'
compile 'com.github.derjust:spring-data-dynamodb:4.3.1'
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
DynamoDB配置
@Configuration
@EnableDynamoDBRepositories(basePackages = "test.project.agent.agent")
public class DynamoConfig
{
@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);
}
}
代理人(实体)
@DynamoDBTable(表名 = "代理")
公开课代理
{
私有字符串代理号码;
私有整数 ID;
私营企业;
私有 AgentState 代理状态;
@DynamoDBHashKey(attributeName = "agentNumber")
public String getAgentNumber()
{
return agentNumber;
}
public void setAgentNumber(String agentNumber)
{
this.agentNumber = agentNumber;
}
@DynamoDBAttribute(attributeName = "id")
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
@DynamoDBAttribute(attributeName = "business")
public Business getBusiness()
{
return business;
}
public void setBusiness(Business business)
{
this.business = business;
}
@DynamoDBAttribute(attributeName = "agentState")
public AgentState getAgentState()
{
return agentState;
}
public void setAgentState(AgentState agentState)
{
this.agentState = agentState;
}
}
代理存储库
package test.project.agent.agent;
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;
@EnableScan
public interface AgentRepository extends CrudRepository<Agent, String>
{
Agent findByNumber(String number);
}
AgentController(注入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
注入被注释掉,应用程序无法启动。
除了上述错误之外,我还得到以下异常:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'agentRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)