使用 SpringBoot 设置 DynamoDB

Vin*_*ino 2 java amazon-dynamodb spring-boot

我正在尝试使用 SpringBoot 设置本地 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}"
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

代理人(实体)

@DynamoDBTable(tableName = "agent") 公共类 Agent { private String agentNumber; 私有整数 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

代理存储库

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

导致此错误的原因是什么以及如何解决此问题?

注意:即使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)

Vin*_*ino 5

该错误消息相当具有误导性。真正的原因是spring-data-dynamodb库与spring-boot版本不匹配。页面列出了版本要求。所以就我而言,修复方法是使用更新spring-data-dynamodb

所以在 Gradle 文件中(使用版本 5.0.4 而不是 4.3.1)

compile group: 'com.github.derjust', name: 'spring-data-dynamodb', version: '5.0.4'
Run Code Online (Sandbox Code Playgroud)