1

特别是我有一个属于多项目构建的子项目。
该子项目没有SpringBootApplication自己的。
经过一些研究,我发现在 ClassPath@SpringBootConfiguration中搜索了
但是现在我无法进一步了解,因为找不到DataSource在中配置的那个。application.yml

抛出以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'de.acme.storage.DBConnectionTest': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    ...

我的期望是看到输出:DataSource: HikariDataSource (HikariPool-1)正如关于 Spring Boot 2 和 Hikari 的指南中所解释的那样。
但是,本指南已经对 SpringBootApplication 类中的“测试”感到满意。

我的错在哪里?

子项目的结构如下:

src
+---main
|   \---java
|   |   \---de
|   |       \---acme
|   |           \---storage
|   |               +---model
|   |               |       User.java
|   |               |       
|   |               \---mysql
|   |                       UserDAOImpl.java
|               
\---test
    +---java
    |   \---de
    |       \---acme
    |           \---storage
    |                   DBConnectionTest.java
    |                   TestConfig.java
    |                   
    \---resources
            application.yml

子项目的build.gradle:

plugins {
    id 'org.springframework.boot'
}

apply plugin: 'io.spring.dependency-management'

dependencies {
    implementation project(':api'),
            'javax.inject:javax.inject:1',
            'org.springframework.boot:spring-boot-starter-data-jpa'

    runtime 'mysql:mysql-connector-java'
}

主项目中的 build.gradle 包含此依赖项部分:

dependencies {
    implementation 'org.slf4j:slf4j-api'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'junit', module: 'junit' //by both name and group
    }
    testImplementation 'org.junit.jupiter:junit-jupiter-api',
            'org.junit.jupiter:junit-jupiter-params'

    testRuntime 'org.junit.jupiter:junit-jupiter-engine'
}

application.yml 内容:

spring:
  datasource:
    url: jdbc:mysql://localhost:13306/testdb
    username: test-user
    password: geheim
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect
        format_sql: true
        id:
          new_generator_mappings: false

JUnit 测试:

@SpringBootTest
class DBConnectionTest {
    private static final Logger LOG = LoggerFactory.getLogger(DBConnectionTest.class);

    @Inject
    private DataSource dataSource;

    @Test
    void test() {
        LOG.debug("DataSource: {}", dataSource);
        assertThat(dataSource).as("Es gibt eine Datasource").isNotNull();
    }
}

@SpringBootConfiguration最后是带有注释的空配置:

package de.acme.storage;

import org.springframework.boot.SpringBootConfiguration;

@SpringBootConfiguration
class TestConfig {
}
4

1 回答 1

1

只是少了一个小东西,注释@EnableAutoConfiguration。以为这会由@SpringBootConfiguration...完成

package de.acme.storage;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

@SpringBootConfiguration
@EnableAutoConfiguration
class TestConfig {
}
于 2019-04-15T09:39:24.413 回答