I am trying to pull data from application.properties file in Spring Boot
application.properties
host=localhost:8080
accountNumber=1234567890
TestController.java
@RestController
public class TestController {
private Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired
private TestService testServiceImpl;
@Value("${host}")
private String host;
@RequestMapping("/test")
public String test() {
testServiceImpl = new TestService();
return testServiceImpl.getValue();
}
TestServiceImpl.java
@Service
public class TestServiceImpl implements TestService{
@Value("${accountNumber}")
public String value;
public String getValue(){
return value;
}
When I do a REST call to localhost:8080/test, I get a null value.
TestServiceImpl is instantiated however, @Value does not seem to work.
Am I missing anything?
SOLUTION:
All I had to do was to remove the line testServiceImpl = new TestService();
I am assuming it was doing that because new TestService() was overwriting the autowired instance of TestService
answer. SO encourages people to answer your own question. stackoverflow.com/help/self-answertestServiceImpl, spring container will create the bean for you and you said NO by creating instance yourself withnew TestService()meddling with spring.