我有一个带有“url”变量的 REST 消费者类。我不想将端点 url 直接输入到变量中,而是将实际的 url 字符串外部化为例如application.properties 文件。那么我如何从代码中的变量引用 url 字符串呢?
我知道,这是一个 super 简单的问题,但我在谷歌上找不到任何东西:)
最佳答案
在 SpringBoot 中执行此操作的另一种类型更安全的方法是使用 @ConfigurationProperties(prefix="some_prefix")
。您在 application.properties endpoint.url=example.com
中声明变量。然后在你的类里面你做类似的事情:
@Service
@ConfigurationProperties(prefix="endpoint")
public class exampleClass {
private String url; //variable name has to match name of the variable definied in application.properties
//getter and setter for url is mandatory!
}
关于spring - 如何将 Spring Boot 中的 url 外部化到 application.properties 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258197/