我一直在假设一些东西,现在发现它是不正确的。我的 spring 上下文中有以下配置属性声明:
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchContextAttributes" value="true" />
<property name="contextOverride" value="true" />
<property name="locations">
<list>
<value>classpath:/app.properties</value>
</list>
</property>
</bean>
我认为 app.properties
中的值会覆盖我的系统属性,因此我可以像这样在我的 Java 类中直接访问它们:
String someThingFromPropertyFile = System.getProperty("nameFromPropertyFile");
当然,我到处都会遇到空指针异常。现在我在这里询问如何从您的应用程序(您的应用程序的 Java 类部分)访问您的应用程序属性。
有没有比下面这个更好的方法(我不是说它不好)。
最佳答案
在应用上下文中:
<context:property-placeholder location="classpath:your.properties" ignore-unresolvable="true"/>
然后在 java 中你可以这样做:
@Value("${cities}")
private String cities;
your.properties 包含这个:
cities = my test string
关于java - 从java类中的spring文件访问属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803681/