简体   繁体   中英

Not able to read value from properties file in Spring Boot using @Value annotation

I am not able to read a property from properties file through Spring Boot. I have a REST service which is working through the browser and Postman both and returning me a valid 200 response with data.

However, I am not able to read a property through this Spring Boot client using @Value annotation and getting following exception.

Exception:

helloWorldUrl = null
Exception in thread "main" java.lang.IllegalArgumentException: URI must not be null
    at org.springframework.util.Assert.notNull(Assert.java:115)
    at org.springframework.web.util.UriComponentsBuilder.fromUriString(UriComponentsBuilder.java:189)
    at org.springframework.web.util.DefaultUriTemplateHandler.initUriComponentsBuilder(DefaultUriTemplateHandler.java:114)
    at org.springframework.web.util.DefaultUriTemplateHandler.expandInternal(DefaultUriTemplateHandler.java:103)
    at org.springframework.web.util.AbstractUriTemplateHandler.expand(AbstractUriTemplateHandler.java:106)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:612)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
    at com.example.HelloWorldClient.main(HelloWorldClient.java:19)

HelloWorldClient.java

public class HelloWorldClient {

    @Value("${rest.uri}")
    private static String helloWorldUrl;

    public static void main(String[] args) {
        System.out.println("helloWorldUrl = " + helloWorldUrl);
        String message = new RestTemplate().getForObject(helloWorldUrl, String.class);
        System.out.println("message = " + message);
    }

}

application.properties

rest.uri=http://localhost:8080/hello

There are several problems in your code.

  1. From the samples you posted, it seems that Spring is not even started. The main class should run the context in your main method.

     @SpringBootApplication public class HelloWorldApp { public static void main(String[] args) { SpringApplication.run(HelloWorldApp.class, args); } } 
  2. It isn't possible to inject a value into a static field. You should start with changing it into a regular class field.

  3. The class must be managed by Spring container in order to make value injection available. If you use default component scan you can simply annotate the newly created client class with the @Component annotation.

     @Component public class HelloWorldClient { // ... } 

    If you don't want to annotate the class you can create a bean in one of your configuration classes or your main Spring Boot class.

     @SpringBootApplication public class HelloWorldApp { // ... @Bean public HelloWorldClient helloWorldClient() { return new HelloWorldClient(); } } 

    However, if you are the owner of the class, the first option is preferable. No matter which way you choose, the goal is to make the Spring context aware of class existence so the injection process can happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM