0

I am trying to read a simple config.properties file but i keep getting null as the value.

I have config.properties file under the root (at the same level with pom.xml) I have only one line in config.properties.

KEY=baran

And i have an AppConfig class like below

package tr.com.simroll.ada.rvm.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {

    @Value("${KEY}")
    private String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

I am trying to access from my controller like this

@Controller
@CrossOrigin(origins = "*")
public class MovieCategoryController {

    @RequestMapping(value = "/api/movieCategory/list", method = RequestMethod.GET)
    @ResponseBody
    public String listMovieCategories(ModelMap model, HttpServletResponse res, HttpServletRequest req) {


        AppConfig config = new AppConfig();
        System.out.println(config.getTest());

        return "test";
    }
Jobin
  • 5,610
  • 5
  • 38
  • 53
rematnarab
  • 1,277
  • 4
  • 22
  • 42

1 Answers1

1

Here the problem is that you are creating a new instance of AppConfig in controller, Instead of that you should inject AppConfig which is created by Spring.

Try add the following to your controller

@Autowired
AppConfig appConfig;

So it becomes..

@Controller
@CrossOrigin(origins = "*")
public class MovieCategoryController {

 @Autowired
 AppConfig appConfig;

    @RequestMapping(value = "/api/movieCategory/list", method = RequestMethod.GET)
    @ResponseBody
    public String listMovieCategories(ModelMap model, HttpServletResponse res, HttpServletRequest req) {

        System.out.println(appConfig.getTest());

        return "test";
    }
Jobin
  • 5,610
  • 5
  • 38
  • 53