- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道 System.getenv(/**vName**/)
获取附加了 vName 变量的环境属性,但我很好奇我们是否也可以使用下面的方法来实现相同的结果?
@Autowired
private Environment environment;
environment.getProperty(/**vName**/);
有什么区别吗?我们什么时候应该使用哪一个?
代码正在运行
@Autowired
private Environment env; <---**I am not sure how this env is getting used?**
@Value("${someURLVriable}")
String APIURLTest;
@Bean(name = "restTemplate")
public RestTemplate restTemplate(RestTemplateBuilder builder) {
String env = System.getenv("environment");<--**eclipse giving warning here.**
String baseURL = System.getenv("illustrationAPIURL");
if (baseURL == null) {/** If baseURL comes null i.e it must be on local/test Env **/
baseURL = APIURLTest;
env = "DEV";
}
log.info("Environment loaded : {}.", env);
log.info("loaded apiURL : {}.", baseURL);
return builder.rootUri(baseURL).build();
}
最佳答案
使用System.getenv("XYZ")
,您将只访问操作系统的环境变量。如果您使用Spring的Environment
抽象,您不仅可以在操作系统的环境变量中搜索您的属性,还可以在以下位置搜索您的属性:
Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on. The role of the environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them. - JavaDoc of the Environment interface
因此 Spring 实际上会根据正在运行的应用程序的 Activity 配置文件以及更多地方(例如,)搜索您请求的属性。 JNDI 或 ServletContextParameters。如果您不需要在多个地方额外搜索 Spring,只需注入(inject)一个环境变量(提示:系统属性 -DmyName= 之间存在差异foo
和环境变量 FOO_BAR=duke
),那么你可以使用 System.getenv("XYZ")
关于java - System.getenv(/**vName**/) 和 Autowiring 环境之间的区别和执行environment.getProperty ("myProp");,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59607440/
这是示例 igraph 对象: library(igraph) #dummy data d # ..- attr(*, "graph")= chr "8a71f114-20e8-11e6-bb8
我知道 System.getenv(/**vName**/) 获取附加了 vName 变量的环境属性,但我很好奇我们是否也可以使用下面的方法来实现相同的结果? @Autowired priva
我是一名优秀的程序员,十分优秀!