In particular I have a subproject which belongs to a Multi-Project-Build.
The subproject has no SpringBootApplication
of its own.
After some research I found out that in ClassPath @SpringBootConfiguration
is searched for
But now I can't get any further because the DataSource
that is configured in the application.yml
is not found.
The following Exception is thrown:
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)
...
My expectation was to see the output: DataSource: HikariDataSource (HikariPool-1)
as explained in a guide about Spring Boot 2 and Hikari.
However, this guide has been satisfied with a "test" in the SpringBootApplication class.
Where's my fault?
The structure of the subproject is as follows:
src
+---main
| \---java
| | \---de
| | \---acme
| | \---storage
| | +---model
| | | User.java
| | |
| | \---mysql
| | UserDAOImpl.java
|
\---test
+---java
| \---de
| \---acme
| \---storage
| DBConnectionTest.java
| TestConfig.java
|
\---resources
application.yml
The build.gradle of the subproject:
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'
}
The build.gradle in the main project contains this dependencies section:
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'
}
The application.yml content:
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
The JUnit test:
@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();
}
}
And finally the empty configuration with @SpringBootConfiguration
annotation:
package de.acme.storage;
import org.springframework.boot.SpringBootConfiguration;
@SpringBootConfiguration
class TestConfig {
}
There was just one little thing missing, the annotation @EnableAutoConfiguration
. Thought this would be done by @SpringBootConfiguration
...
package de.acme.storage;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@SpringBootConfiguration
@EnableAutoConfiguration
class TestConfig {
}