java.util.Properties:如何从spring配置xml中的属性检索值
我有一个java.util.Properties对象,其中几乎没有键值对。我试图在spring配置文件中使用这个属性对象,即在spring配置中定义键,在运行时,它应该从属性对象中获取值 例如:java.util.Properties:如何从spring配置xml中的属性检索值,java,spring,properties,configuration,Java,Spring,Properties,Configuration,我有一个java.util.Properties对象,其中几乎没有键值对。我试图在spring配置文件中使用这个属性对象,即在spring配置中定义键,在运行时,它应该从属性对象中获取值 例如: <bean id="test" class="com.sample.Test"> <constructor-arg value="${PROPERTY_KEY} /> </bean> 或 首先必须创建bean来访问属性文件,如下所示 <bean
<bean id="test" class="com.sample.Test">
<constructor-arg value="${PROPERTY_KEY} />
</bean>
或
首先必须创建bean来访问属性文件,如下所示
<bean id="appProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:localhost-mysql.properties</value>
<value>classpath*:mail-server.properties</value>
</list>
</property>
</bean>
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="appProperties" />
</bean>
<bean id="mailServerSettings" class="com.ratormonitor.app.context.EmailSettings">
<property name="host" value="${mail.server.host}" />
<property name="port" value="${mail.server.port}" />
<property name="username" value="${mail.server.username}" />
<property name="password" value="${mail.server.password}" />
<property name="requestContextPath" value="${request.context.path}" />
</bean>
classpath*:localhost-mysql.properties
classpath*:mail-server.properties
接下来,您可以从属性文件访问键值对,如下所示
<bean id="appProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:localhost-mysql.properties</value>
<value>classpath*:mail-server.properties</value>
</list>
</property>
</bean>
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="appProperties" />
</bean>
<bean id="mailServerSettings" class="com.ratormonitor.app.context.EmailSettings">
<property name="host" value="${mail.server.host}" />
<property name="port" value="${mail.server.port}" />
<property name="username" value="${mail.server.username}" />
<property name="password" value="${mail.server.password}" />
<property name="requestContextPath" value="${request.context.path}" />
</bean>
希望此代码能解决您的问题 您可以使用Spring表达式语言(SpEL)在Spring配置xml文件中获取java对象值
例如:
<property name="misfireInstruction"
value="#{T(org.quartz.CronTrigger).MISFIRE_INSTRUCTION_FIRE_ONCE_NOW}"/>
我就是这样做的:
正如我所说,我有一个java.util.Properties对象。然后我创建了一个CustomProperty类,它扩展了PropertySource>
public class CustomPropertySource extends PropertySource<Map<String, Object>>
然后在spring配置文件中,我必须添加以下内容:
<context: property-placeholder ignore-unresolvable="true"/>
因此,通过这种方式,我可以获取spring配置文件中定义的键的值,就像从属性文件获取值一样
谢谢,
拉胡尔
。像这样的东西应该能奏效。谢谢Denium。我会试试这个。但是我不知道怎么做,因为我在xml中没有属性bean id,我也不能创建一个属性bean id?为什么不能用XML创建它呢?你能为你的问题添加更多信息吗?他明确表示他不想使用文件。嗨,正如Denium正确地说的,我不想使用任何属性文件来检索值。嗨,Senthil,谢谢你的回答,但是正如问题中提到的,我不想使用任何属性文件。
<context: property-placeholder ignore-unresolvable="true"/>