【发布时间】:2018-12-07 11:47:04
【问题描述】:
我正在尝试使用 SpringBoot 设置本地 DynamoDB 实例。我正在关注this,但使用的是 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 作为无参数构造函数。不确定歧义在哪里。
以下是我的代码:
Gradle 文件
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}"
}
}
DynamoDBConfig
@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(tableName = "agent") 公共类代理 { 私人字符串代理号码; 私人整数 id; 私营企业; 私有AgentState 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)
【问题讨论】:
-
你的 id 是整数,所以你应该使用
public interface AgentRepository extends CrudRepository<Agent, Integer>。告诉我它是否有效。 -
id不是表的主键。主键其实是agentNumber(哈希键),所以我认为定义是正确的。我尝试了您的建议,但没有奏效。 -
哦,对不起,我的 id 名字让我误会了
-
能否请您在注入
agentRepository的位置添加代码? -
添加了请求的代码
标签: java spring-boot amazon-dynamodb