【问题标题】:Setting up DynamoDB with SpringBoot使用 Spring Boot 设置 DynamoDB
【发布时间】: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&lt;Agent, Integer&gt; 。告诉我它是否有效。
  • id 不是表的主键。主键其实是agentNumber(哈希键),所以我认为定义是正确的。我尝试了您的建议,但没有奏效。
  • 哦,对不起,我的 id 名字让我误会了
  • 能否请您在注入agentRepository的位置添加代码?
  • 添加了请求的代码

标签: java spring-boot amazon-dynamodb


【解决方案1】:

错误信息相当具有误导性。实际原因是spring-data-dynamodb 库与spring-boot 版本不匹配。 This 页面列出了版本要求。所以在我的情况下,修复是使用更新spring-data-dynamodb。

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

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

【讨论】:

  • Yesssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss超级明星,为我做到了,非常感谢。
  • 方法 * in * 的参数 * 需要一个无法找到的存储库类型的 bean 是我的错误消息,这修复了它。谢谢!
【解决方案2】:

我们创建了一篇端到端开发文章,展示了如何创建与 DynamoDB 交互的 Spring Boot 应用程序。在此应用程序中,使用了 Java V2 DynamoDB API(和其他 AWS APIS)。此外,它还展示了 V2 增强型客户端的使用 - DynamoDB V2 API 的一项新功能。

此外,它还演示了如何将应用程序部署到 Elastic Beanstalk。

你可以在这里找到开发文章:https://github.com/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_dynamodb_web_app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2016-08-28
    • 1970-01-01
    • 2018-06-16
    • 2015-12-28
    • 1970-01-01
    • 2018-07-05
    • 2018-11-29
    相关资源
    最近更新 更多