0

I am trying to access a custom property from application.properties (present in src/main/resources) of my Spring Boot rest application and trying to access that value from my controller

Spring boot application class location "src/main/java/com/myapp/FileReaderApp.java"

package com.myapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FileReaderApp {

public static void main(String[] args) {
    SpringApplication.run(FileReaderApp.class, args);
    }
}

Controller class location : "src/main/java/com/myapp/controller/FileReaderController.java"

@RestController
@RequestMapping("/counter-api")
public class FileReadController {

    @Value("${fileLocation}")
    static private String fileLocation;
    //do other actions 
   }

I found out i couldnt do it at all and everytime fileLocation is coming as null.

My application.properties file has only one property, fileLocation.

Could anyone please advise where I am going wrong in this case and what is the best way to access an application property value in REST Controller class?

dbas
  • 1
  • 1
  • 5
  • possible duplicate of http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field – iamiddy Feb 01 '17 at 02:41
  • @iamiddy the link you provided is similar but didnt help in solving my problem. do you have any other option in mind? – dbas Feb 01 '17 at 02:57
  • thought your issue is caused by a static variable? – iamiddy Feb 01 '17 at 02:59
  • Just tried with the following code as well, it didnt help :( it would be great if someone can help "@RestController @RequestMapping("/counter-api") public class FileReadController { @Autowired private Environment environment; private FileReaderService service = new FileReaderServiceImpl(environment.getProperty("fileLocation"));" – dbas Feb 01 '17 at 03:02
  • are you able to autowire any property value? – iamiddy Feb 01 '17 at 03:03
  • @iamiddy yes tried the above, didnt work – dbas Feb 01 '17 at 03:04
  • @Value("${fileLocation}") private String fileLocation; – iamiddy Feb 01 '17 at 03:05
  • No @iamiddy didnt work sorry :( where do you think i should put that in? – dbas Feb 01 '17 at 03:29
  • @ManishaB Could you please share the application.properties file content? – Ravindra Devadiga Feb 01 '17 at 04:37
  • @RavindraDevadiga fileLocation=/Users/mymachine/Documents/workspace/TestStreamsJava8/src/main/resources/file/input.txt – dbas Feb 01 '17 at 04:57
  • I just created a quick spring-boot app from start.spring.io and added your controller with the fileLocation property and it worked fine. Only difference was `static`; my variable isn't static. You're code looks fine. It has to be something you're not showing us. – Gregg Feb 01 '17 at 05:02
  • @Gregg thanks! can you check if you are able to access that variable "fileLocation" , i.e the value is not populated as null? – dbas Feb 01 '17 at 05:17
  • Pretty sure that's what I just said; yes, I can access the variable and see it's value, print it, whatever. – Gregg Feb 01 '17 at 05:18
  • Possible duplicate of [Spring: How to inject a value to static field?](http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field) – Strelok Feb 01 '17 at 06:36

1 Answers1

3

You're injecting into a static field. This is not supported.

Strelok
  • 50,229
  • 9
  • 102
  • 115