What are the necessary and sufficient conditions for the injection of property in Spring Boot?

advertisements

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


Your @configurationProperties on your class should have @Bean or @Component annotation for the spring to inject property values . More information about properties can be found here

@ConfigurationProperties
@Component // or @Bean
public class Database {