0

All dear Master, please help me Why my @Value failed to load my application.properties, do i got to add any configuration? here my code My DataprocClient.java

@Component
public class DataprocClient {
    @Value("${ipdataprocessing}")
    private String ipDataProcessing;

    public ResponseModel reqDataproc(String uri,MultiValueMap<String, String> post) {
        System.out.println("ipDataProcessing"+ipDataProcessing);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(post, headers);

        RestTemplate restTemplate = new RestTemplate();

        ResponseModel responseModel = new ResponseModel();
        responseModel = restTemplate.postForObject(ipDataProcessing, request, ResponseModel.class);

        return responseModel;
    }
}

my application.properties

dataprocurl=http://10.245.4.132:8100/api/kswpService
ipdataprocessing=http://127.0.0.1:8080
server.port=80
server.max-http-header-size=10000000
spring.boot.admin.url=http://localhost:8888
management.security.enabled=false
spring.application.name=DataProcess-DB

i am new in springboot please help me.......

ikhsan
  • 13
  • 5

1 Answers1

0

The @Value annotation only works if your class is instantiated through Spring's Dependency Injection mechanism, for example if it is annotated with @Component (or @Controller or a couple of others) and then injected elsewhere with @Autowired.

In your case, the class is not a @Component, and I assume you instantiate it manually (with new DataprocClient()), and thus the field annotated with @Value is not filled during bean instantiation.

  • i have add @Component on my DataprocClient.java and its still null – ikhsan Sep 02 '20 at 01:08
  • @ikhsan, did you read the rest of my answer? –  Sep 02 '20 at 01:14
  • iam new in java i only know to initiate class like this DataprocClient dataprocclient = new DataprocClient(); – ikhsan Sep 02 '20 at 02:32
  • @ikhsan, yes, that is exactly what you must not do when using Spring annotations like `@Value` and `@Autowired`. –  Sep 02 '20 at 02:44