使用Spring在战后读取属性文件
问题内容:
enter code here
我在etc文件夹中放置了一个属性文件。每个子模块中都包含“
myapplication.properties”和其他几个属性文件。我正在尝试执行以下操作
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>${config}</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
我正在尝试做 mvn -Dconfig =〜/ my.properties jetty:run
这些属性是从application.properties中读取的,而不是从config。中读取的。
在运行应用程序时,我得到的$ {jdbc.url}不正确..该URL存在于my.properties中。如何实现?
谢谢
问题答案:
这就是我所要运行的
<bean id="placeholderConfigConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${config}" />
</bean>
并添加-Dconfig=/var//my.properties
MAVEN_OPTS ..并执行 mvn jetty:run
我发现了另一行解决方案..而不是进行冗长的配置
<context:property-placeholder location="file:${config}"/>