在Spring应用程序中加载嵌入式DynamoDB的适当驱动程序是什么?

时间:2017-01-26 15:48:15

标签: spring configuration amazon-dynamodb

我按照各种教程来完成下面的配置。可能还有不正确的配置导致问题。当我运行测试时,我得到:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "test" are currently active).

研究让我相信没有加载适当的驱动程序,我必须将它添加到.properties。这是DynamoDBLocal库中包含的内容吗?我无法在文档中找到它,似乎我唯一的选择是从网络上获取第三方驱动程序。

以下是重要部分:

的pom.xml:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-dynamodb</artifactId>
        <version>1.11.34</version>
    </dependency>
    <dependency>
        <groupId>com.github.derjust</groupId>
        <artifactId>spring-data-dynamodb</artifactId>
        <version>4.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>DynamoDBLocal</artifactId>
        <version>1.10.5.1</version>
    </dependency>

DataSourceConfigLocal:

@Configuration
@EnableWebMvc
@EnableDynamoDBRepositories(basePackages="com.cfa.dao")
@Profile({"local", "test"})
public class DataSourceConfigLocal {

    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.aws.accesskey}")
    private String amazonAWSAccessKey;

    @Value("${amazon.aws.secretkey}")
    private String amazonAWSSecretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        AmazonDynamoDB amazonDynamoDB
                = new AmazonDynamoDBClient(amazonAWSCredentials());

        if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
            amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
        }

        return amazonDynamoDB;
    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(
                amazonAWSAccessKey, amazonAWSSecretKey);
    }
}

IntegrationTest:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Profile("test")
@TestPropertySource(properties = {
        "amazon.dynamodb.endpoint=http://localhost:8000/",
        "amazon.aws.accesskey=x",
        "amazon.aws.secretkey=x" })
public class OrderRequestRepositoryIntegrationTest {

    private DynamoDBMapper dynamoDBMapper;

    @Autowired
    private AmazonDynamoDB amazonDynamoDB;

    @Autowired
    OrderRequestDao orderRequestDao;

    private static final String STORE_NUMBER = "100";

    @Before
    public void setup() throws Exception {
        dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);

        CreateTableRequest tableRequest = dynamoDBMapper
                .generateCreateTableRequest(OrderRequest.class);
        tableRequest.setProvisionedThroughput(
                new ProvisionedThroughput(1L, 1L));
        amazonDynamoDB.createTable(tableRequest);

        dynamoDBMapper.batchDelete(
                (List<OrderRequest>)orderRequestDao.findAll());
    }

    @Test
    public void sampleTestCase() {
        OrderRequest orderRequest = new OrderRequest(STORE_NUMBER);
        orderRequestDao.save(orderRequest);

        List<OrderRequest> result
                = (List<OrderRequest>) orderRequestDao.findAll();

        assertTrue("Not empty", result.size() > 0);
        assertTrue("Contains item with expected cost",
                result.get(0).getStoreNumber().equals(STORE_NUMBER));
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定你是否已经提到了这一点。我正在添加它,因为它可以帮助你。

Test using HTTP and without using HTTP

pom file which has the server runner and sql lite

1)另外,使用JAR的最新版本 1.11.0.1

2)类路径中的SQL Lite lib

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>DynamoDBLocal</artifactId>
    <version>${aws.dynamodblocal.version}</version>
</dependency>

<profile>
            <id>start-dynamodb-local</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>${exec.maven.plugin.version}</version>
                        <executions>
                            <execution>
                                <phase>initialize</phase>
                                <configuration>
                                    <executable>java</executable>
                                    <arguments>
                                        <argument>-cp</argument>
                                        <classpath/>
                                        <argument>-Dsqlite4java.library.path=${basedir}/target/dependencies</argument>
                                        <argument>com.amazonaws.services.dynamodbv2.local.main.ServerRunner</argument>
                                        <argument>-inMemory</argument>
                                        <argument>-port</argument>
                                        <argument>${dynamodb-local.port}</argument>
                                    </arguments>
                                </configuration>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>