Reading values ​​from application.properties Spring Boot

My Spring Boot Application has this application structure:

  • CAC
    • Main
      • Java
      • resources
        • application.properties

This is my application.properties file:

logging.level.org.springframework=TRACE
logging.level.org.hibernate=ERROR
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
#spring.resources.chain.cache=false
#spring.resources.chain.html-application-cache=false
#spring.headers.cache=false
language=java

      

I have a class that requires the use of this language = java property. This is how I try to use it:

public class EntityManager {

    @Value("${language}")
    private static String newLang;

    public EntityManager(){
        System.out.println("langauge is: " + newLang);
    }
}

      

This printable value is always "null" for some reason! I also tried to put this on top of the class declaration:

@PropertySource(value = "classpath:application.properties")

      

+3


source to share


8 answers


This can be achieved in several ways, see below.

@Configuration
@PropertySource("classpath:application.properties")
public class EntityManager {

    @Value("${language}")
    private static String newLang;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

      



OR

@Configuration
@PropertySource("classpath:application.properties")
public class EntityManager {

    @Autowired
    private Environment env;

    public void readProperty() {
        env.getProperty("language");
    }

}

      

+7


source


Lack of stereotype annotation on top of the class



@Component
public class EntityManager {

    @Value("${language}")
    private static String newLang;

    public EntityManager(){
        System.out.println("langauge is: " + newLang);
    }
}

      

+1


source


OK I figured it out with despair. I added

@PropertySource("classpath:application.properties") 

      

but for some reason it still doesn't work.

Then I removed the "static" modifier and it works!

I'm not sure why it works without "static" presence, but it does. If I can explain this to me, that would be great because it is confusing.

+1


source


Sometimes the classpath element for src/main/resources

contains an exclude tag. If application.properties

present in this list of excluded items, then @PropertySource("classpath:application.properties")

it will not be able to find the property file.

Either remove the [.classpath][1]

file exceptions entry for src/main/resources

manually, or use the Configure build path option in Eclipse and go to the Source tab. Then remove the entry to exclude from src

.

+1


source


create you beans

@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationBeansConfig {

    @Autowired
    Environment env;

    @Bean
    public IApplicationBeanService getService(){
        return new ApplicationBeansService(env);
    }
}

      

and then in the service costructor

@Service
public class ApplicationBeansService implements IApplicationBeanService {
...
   public ApplicationBeansService(Environment env){
      ...
      String value = env.getProperty("your.value")
      ...
   }
...
}

      

don't forget to fill in your application.properties:

your.value = some-string-value

      

+1


source


you can try to use @PropertySource

and give the path to the properties file, you can find an example below:

@Component
@PropertySource("classpath:application.properties")
public class EntityManager {

    @Value("${language}")
    private static String newLang;

    public EntityManager(){
        System.out.println("langauge is: " + newLang);
    }
}

      

0


source


This is probably not the exact solution you are looking for, but you can also declare a resource bean source:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

0


source


If you are using Spring Boot you don't need to @PropertySource("classpath:application.properties")

, if you are using Spring parent starter, just remove the keyword static

and it should start working.

0


source







All Articles