I have the following class:
@ConfigurationProperties
public class Database {
...
@Value("${database.driver-class-name}")
public void setDriverClassName(String driverClassName) {
...
}
Have the following dependencies in build.gradle
:
dependencies {
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-configuration-processor')
Have the following line in application.properties
:
database.driver-class-name=com.mysql.jdbc.Driver
Have the following annotations on test class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(DatabaseTest.Context.class)
@EnableConfigurationProperties
public class DatabaseTest {
@Configuration
public static class Context {
@Bean
public Database database() {
Database ans = new Database();
return ans;
}
}
@Autowired
public Database database;
and it still neither inject properties from property file nor swears with some error message.
What else it wants?
What are necessary and sufficient conditions for properties to inject from propery file?
UDPATE
Sample repository: https://github.com/dims12/MinimalRequrementsToInjectPropertiesFromFile
Sorry, found an answer myself.
In the case of Spring Boot, a context configuration class is usually annotated with @SpringBootApplication
which causes many things, including automatic finding an wiring of property files. This is invoked by @EnableAutoConfiguration
, which is contained inside former annotation.
In my case, I have annotated context configuration class with plain-Spring @Configuration
annotation only, because I was in JUnit
test and thought it is not an application. In this case I was to add @EnableAutoConfiguration
explicitly.
Probably I was also allowed to annotate context configuration class with @SpringBootApplication
, but I didn't test it.
@EnableConfigurationProperties
was not needed at all. Also no need for @ConfigurationProperties
, and AutowiredAnnotationBeanPostProcessor
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments