Why @Value in jaca class failed to load properties in application.properties java springboot

huangapple go评论76阅读模式
英文:

Why @Value in jaca class failed to load properties in application.properties java springboot

问题

亲爱的大师,请帮我解决为什么我的 @Value 无法加载我的 application.properties 文件?我需要添加任何配置吗?以下是我的代码:

我的 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 = restTemplate.postForObject(ipDataProcessing, request, ResponseModel.class);

        return responseModel;
    }
}

我的 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

我是 Spring Boot 的新手,请帮帮我……

英文:

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(&quot;${ipdataprocessing}&quot;)
    private String ipDataProcessing;

    public ResponseModel reqDataproc(String uri,MultiValueMap&lt;String, String&gt; post) {
        System.out.println(&quot;ipDataProcessing&quot;+ipDataProcessing);

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

        HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; request = new HttpEntity&lt;&gt;(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.......

答案1

得分: 0

@Value注解仅在通过Spring的依赖注入机制实例化类时起作用,例如,如果该类被标注了@Component(或@Controller等几种注解),然后在其他地方通过@Autowired进行注入。

在您的情况下,该类并未被标注为@Component,我假设您是手动实例化它的(使用new DataprocClient()),因此使用@Value注解标注的字段在Bean实例化过程中不会被填充。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月2日 08:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63697186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定