英文:
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("${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.......
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论