简体   繁体   English

使用 SpringBoot 设置 DynamoDB

[英]Setting up DynamoDB with SpringBoot

I am trying to setup local DynamoDB instance with SpringBoot.我正在尝试使用 SpringBoot 设置本地 DynamoDB 实例。 I am following this but with Gradle.我正在关注这个但是使用 Gradle。

When I try to run my application I get the following exception:当我尝试运行我的应用程序时,出现以下异常:

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)

I do understand that this is a failure to dependency inject due to ambiguity but my AgentRepository as a no-arg constructor.我确实理解这是由于歧义导致的依赖注入失败,但我的AgentRepository作为无参数构造函数。 Not sure where the ambiguity is.不确定歧义在哪里。

The following is my code:以下是我的代码:

Gradle file摇篮档案

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 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);
    }
}

Agent (entity)代理人(实体)

@DynamoDBTable(tableName = "agent") public class Agent { private String agentNumber; @DynamoDBTable(tableName = "agent") public class Agent { private String agentNumber; private Integer id;私有整数 id; private Business business;私营企业; private 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;
    }
}

AgentRepository代理库

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 (Where AgentRepository is injected) 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);
    }
}

What's the cause of this error and how can I resolve this?此错误的原因是什么,我该如何解决?

Note: Even when part where the AgentRepository injected is commented out, the application fails to start.注意:即使注释掉注入AgentRepository的部分,应用程序也无法启动。

In addition to the aforementioned error I do get the following exception too:除了上述错误之外,我也得到以下异常:

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)

The error message is rather misleading.错误消息相当具有误导性。 The actual reason is the spring-data-dynamodb library mismatch with spring-boot version.实际原因是spring-data-dynamodb库与spring-boot版本不匹配。 This page lists the version requirement.页面列出了版本要求。 So in my case the fix was to use an update spring-data-dynamodb .所以在我的例子中,修复是使用更新spring-data-dynamodb

So in Gradle file (using version 5.0.4 as opposed to 4.3.1)所以在 Gradle 文件中(使用版本 5.0.4 而不是 4.3.1)

compile group: 'com.github.derjust', name: 'spring-data-dynamodb', version: '5.0.4'

We created an end to end development article that shows HOW TO create a Spring Boot application that interacts with DynamoDB.我们创建了一篇端到端开发文章,展示了如何创建与 DynamoDB 交互的 Spring Boot 应用程序。 In this application, the Java V2 DynamoDB API (and other AWS APIS) is used.在此应用程序中,使用了 Java V2 DynamoDB API(和其他 AWS APIS)。 Plus it shows use of the V2 Enhanced Client - a new feature of the DynamoDB V2 API.此外,它还展示了 V2 增强客户端的使用——DynamoDB V2 API 的一项新功能。

As well, it demonstrates how to deploy the app to Elastic Beanstalk.此外,它还演示了如何将应用程序部署到 Elastic Beanstalk。

在此处输入图像描述

You can find the development article here: https://github.com/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_dynamodb_web_app您可以在此处找到开发文章: https ://github.com/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_dynamodb_web_app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM