简体   繁体   English

从配置加载凭据

[英]Loading credentials from config

I am writing simple rest client with spring boot. 我正在用spring boot编写简单的rest客户端。 In my service class i am using RestTemplate. 在我的服务类中,我正在使用RestTemplate。 How to avoid hardcoding url and ApiKey? 如何避免硬编码网址和ApiKey? I tried to use application.properties file and Value annotation but i get null pointer exception. 我尝试使用application.properties文件和Value annotation但我得到空指针异常。 Edit: Injected fields via constructor (using Value annotation). 编辑:通过构造函数注入的字段(使用值注释)。 Is it a good practice? 这是一个好习惯吗?

It is wise of you to not want to store url and ap ikey in your application. 你不想在你的应用程序中存储url和ap ikey是明智的。

I recommend having a properties file stored in your home directory and then reference that file at startup time. 我建议将属性文件存储在主目录中,然后在启动时引用该文件。

java -jar app.jar --spring.config.location=classpath:/another-location.properties * java -jar app.jar --spring.config.location=classpath:/another-location.properties *

*Taken from Properties with Spring and Spring Boot . *取自Spring和Spring Boot的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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