6

My Spring boot app has this application structure:

  • src
    • 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 which requires the use of that language=java property. This is how I am trying to use it:

public class EntityManager {

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

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

That printed value is always "null" for some reason! I have also tried putting this on top of the class declaration:

@PropertySource(value = "classpath:application.properties")
Karan
  • 1,335
  • 2
  • 14
  • 29

12 Answers12

18

It can be achieved in multiple ways, refer 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");
    }

}
  • 1
    It is still not working with the `@PropertySource` annotation. I am not sure why but I guess it cannot find the application.properties file? Is there a way to check what path the "classpath" refers to? – Karan Aug 01 '17 at 17:05
  • @aBrokenSniper, is application.properties available in classpath? what is the path of application.properties in your project? – Anil Kumar Athuluri Aug 01 '17 at 17:12
  • @aBrokenSniper, alternatively you can use file path as well.. @PropertySource("file:/path/to/application.properties") – Anil Kumar Athuluri Aug 01 '17 at 17:14
  • I put the application.properties in the default location suggested by spring docs: under src>main>resources>application.properties. It should be picked up by default right? – Karan Aug 01 '17 at 17:15
  • Yes, it should be picked up by default. If it doesn't then, try with file path as I commented above – Anil Kumar Athuluri Aug 01 '17 at 17:19
  • Actually I am fairly certain it is being read as I changed the logging level in that application.properties file and the logging level changed for my app. Could it be the way I declared the variable in the file? – Karan Aug 01 '17 at 17:24
  • @PropertySource("classpath:application.properties") have you had it like this? – Anil Kumar Athuluri Aug 01 '17 at 17:27
  • Yep. Also I just figured it out (still not sure why it works)! See my answer below! Thank you for your help! – Karan Aug 01 '17 at 17:30
5

If you are using Spring boot you do not need @PropertySource("classpath:application.properties") if you are using spring boot starter parent , just remove the static keyword and it should start working.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
3

Missing stereotype annotation on top of class

@Component
public class EntityManager {

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

    public EntityManager(){
        System.out.println("langauge is: " + newLang);
    }
}
Barath
  • 5,093
  • 1
  • 17
  • 42
  • Does it have to be a component? I was reading this tutorial: [link](http://memorynotfound.com/spring-boot-passing-command-line-arguments-example/) and they didn't use a Component. Is there any way I can grab the value from the properties file without using any stereotype annotations for the class? – Karan Aug 01 '17 at 04:16
  • it can be any one of the spring stereotype annotations. In that case you have get it from Environment by accessing the ApplicationContext statically inside your class or else make use of System.getProperty interms of JVM property. – Barath Aug 01 '17 at 04:16
3

Sometimes, the classpath entry for src/main/resources contains an exclude tag. If application.properties is present in this list of excluded items then @PropertySource("classpath:application.properties") will not be able to find the property file.

Either remove the exclude entry from [.classpath][1] file for src/main/resources manually, or use the Configure build path option in Eclipse and go to Source tab. Then remove the exclude entry from src.

Antimony
  • 2,230
  • 3
  • 28
  • 38
Mayank Madhav
  • 429
  • 1
  • 7
  • 19
1

Probably not the exact solution you're looking for, but you can also declare a property source bean:

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

Ognjen Mišić
  • 1,219
  • 17
  • 37
1

OK I figured it out with acts of desperation. I added the

@PropertySource("classpath:application.properties") 

attribute but it still wasn't working for some reason.

I then deleted the "static" modifier and it worked!

I am not sure why it works without "static" being there but it does. If can explain it to me that would be wonderful because this is all confusing.

Karan
  • 1,335
  • 2
  • 14
  • 29
  • 1
    https://stackoverflow.com/questions/10938529/why-cant-we-autowire-static-fields-in-spring – Morfic Aug 02 '17 at 11:52
1

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 service costructor

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

don't forget fill your application.properties:

your.value = some-string-value
BazSTR
  • 129
  • 3
1

As provided in the 2nd answer above I was indeed using the spring boot, so removed the static keyword and that resolved my issue. Make sure you don't have static for the property defined with @Value.

VC2019
  • 427
  • 1
  • 5
  • 9
1

The value from the properties file is ALWAYS null IF you are trying to read it BEFORE the Spring Bean has been fully initialized, for example, when calling the value from the constructor. This was the case for me.

To get past this, use the @PostConstruct annotation like this:

@Configuration
public class CustomConfiguration {


     Logger logger = Logger.getLogger(CustomConfiguration.class.getSimpleName());

     @Value("${test.value}")
     private String testValue;

      @PostConstruct
      public void initializeApplication() {
        logger.info("============================================>"+testValue);
      }

Just make sure you are not trying to use the value of testValue anywhere else before initializeApplication is called.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
0

you can try to use @PropertySource and give it the path to property file, you can find the sample below:

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

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

    public EntityManager(){
        System.out.println("langauge is: " + newLang);
    }
}
Mayank Sharma
  • 403
  • 2
  • 2
0

in my case, the same problem had a slightly different cause - i had a class with a static instance variable (the old usual way we used to implement a Singleton pattern), into which i wanted to inject an entry from application.properties (probably the "static" thing was why the property could not be found - with no errors no anything :/).

moreover this class was in a totally different package from the class declared as @SpringBootApplication. i understood that package names and locations have to be taken into serious consideration with Spring Boot and automatic component scanning. my 5 cent

hello_earth
  • 1,442
  • 1
  • 25
  • 39
0

You do not need to specify the properties file if you are using the default application.properties file in src/main/resources directory. It will be auto-detected.

You need to add @Value in the constructor as the value injection doesn't happen before constructor is called.

    private static String newLang;

    public EntityManager(@Value("${language}") newLang){
        this.newLang=newLang;
    }
pallavi
  • 95
  • 2
  • 9