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

Dims

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

Dims

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring boot property injection failed

From Dev

Spring boot property injection to JPA entity definitions

From Dev

What requirements are necessary and sufficient for an ActiveX control to be used directly on an Excel worksheet?

From Dev

spring-boot property injection not working in custom @Configuration class

From Dev

Secondary type dependency injection does not work in spring boot

From Dev

What is the proper spring boot way to apply dependency injection

From Dev

spring boot dependency injection

From Java

What is this spring.jpa.open-in-view=true property in Spring Boot?

From Dev

Necessary and Sufficient vs Soundness and Completeness

From Java

conditional repository injection - Spring Boot

From Java

Keeping only the "Necessary and Sufficient features" in a Dataframe

From Dev

Is it sufficient to trigger notifyUpdateCell() or is it necessary to trigger clearAllCachedResultValues()

From Dev

Spring Property Injection with Play2 Framework

From Dev

Resource injection in Spring Boot not working with gradle

From Dev

Spring Boot bean creation/injection issue

From Dev

spring boot :Injection of autowired dependencies failed;

From Dev

Spring Boot bean creation/injection issue

From Dev

Spring Boot Constructor Based Dependency Injection not working

From Java

Are PDO prepared statements sufficient to prevent SQL injection?

From Dev

Spring with JUnit Testing and Dependency Injection does not work

From Dev

Pass system property to spring boot

From Dev

spring-boot property placeholder

From Dev

spring-boot property placeholder

From Dev

Accessing property file in spring boot

From Dev

Why isn't necessary @Repository for this Spring Boot web app?

From Dev

What is the point of dependency injection as used in Spring?

From Dev

How to get a function to execute until the conditions are not sufficient

From Dev

What are the necessary js files for semantic-ui dropdown to work properly

From Java

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

Related Related

  1. 1

    Spring boot property injection failed

  2. 2

    Spring boot property injection to JPA entity definitions

  3. 3

    What requirements are necessary and sufficient for an ActiveX control to be used directly on an Excel worksheet?

  4. 4

    spring-boot property injection not working in custom @Configuration class

  5. 5

    Secondary type dependency injection does not work in spring boot

  6. 6

    What is the proper spring boot way to apply dependency injection

  7. 7

    spring boot dependency injection

  8. 8

    What is this spring.jpa.open-in-view=true property in Spring Boot?

  9. 9

    Necessary and Sufficient vs Soundness and Completeness

  10. 10

    conditional repository injection - Spring Boot

  11. 11

    Keeping only the "Necessary and Sufficient features" in a Dataframe

  12. 12

    Is it sufficient to trigger notifyUpdateCell() or is it necessary to trigger clearAllCachedResultValues()

  13. 13

    Spring Property Injection with Play2 Framework

  14. 14

    Resource injection in Spring Boot not working with gradle

  15. 15

    Spring Boot bean creation/injection issue

  16. 16

    spring boot :Injection of autowired dependencies failed;

  17. 17

    Spring Boot bean creation/injection issue

  18. 18

    Spring Boot Constructor Based Dependency Injection not working

  19. 19

    Are PDO prepared statements sufficient to prevent SQL injection?

  20. 20

    Spring with JUnit Testing and Dependency Injection does not work

  21. 21

    Pass system property to spring boot

  22. 22

    spring-boot property placeholder

  23. 23

    spring-boot property placeholder

  24. 24

    Accessing property file in spring boot

  25. 25

    Why isn't necessary @Repository for this Spring Boot web app?

  26. 26

    What is the point of dependency injection as used in Spring?

  27. 27

    How to get a function to execute until the conditions are not sufficient

  28. 28

    What are the necessary js files for semantic-ui dropdown to work properly

  29. 29

    What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

HotTag

Archive