【问题标题】:How to use a properties file in Spring beans XML file?如何在 Spring beans XML 文件中使用属性文件?
【发布时间】:2017-04-30 18:41:21
【问题描述】:

我想在 Spring XML 配置文件中提供一些属性文件。例如在hello.xml:

<bean id="theFoo" class="learnspring.Foo">
    <property name="color" value="${foo.color}"/>
</bean>

在 Java 代码中:

ApplicationContext ac = new ClassPathXmlApplicationContext("hello.xml");
File props = new File("path/to/hello.properties");
File moreProps = new File("path/to/more.properties");
// What to do here?
Foo foo = (Foo)ac.getBean("theFoo");
System.out.println(foo.getColor());

hello.properties:

foo.color = blue

如何使属性文件可用于 Spring 对象定义?

更新

我正在移植一些旧的 Spring 代码。 (2.5 版)看起来有点像这样:

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(xmlFile));
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
cfg.setLocations(new Resource[] {
    new ClassPathResource(propsResourcePath),
    new FileSystemResource(propsFile)) });

cfg.postProcessBeanFactory(factory);
new GenericApplicationContext(factory);

此代码已被标记为已弃用,并且我遇到了其他问题,因此我尝试将其移植到新方式。

【问题讨论】:

标签: java spring spring-3


【解决方案1】:

您可以在 PropertyPlaceholderConfigurer 的帮助下阅读 java 代码中的 more.properties。不知道为什么你真的需要读取为文件对象。

在 hello.xml 文件中

<!-- Loading all properties files from classpath -->
    <bean id="myProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:hello.properties</value>
                <value>classpath:more.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    </bean>
    <bean id="theFoo" class="learnspring.Foo">
        <property name="color" value="${foo.color}"/>
        <property name="shape" value="${foo.shape}"/>
    </bean>

在 hello.properties 文件中

 foo.color=blue

在 more.properties 文件中

foo.shape=square

在 Java 代码中

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("hello.xml");
Foo foo = (Foo) ctx.getBean("theFoo");
System.out.println("Color : " + foo.getColor() +" Shape : " + foo.getShape());

【讨论】:

  • 文件对象是因为我正在使用遗留代码。
  • 好的。您是否试图在不使用弹簧的情况下看到类似下面的内容? mkyong.com/java/java-properties-file-examples 如果您需要将这些属性加载到 Foo 对象,您可以自动装配它并设置值。
  • 我正在移植旧的 Spring 代码。我将其添加到“更新”下的问题中。
【解决方案2】:

如果您还想遍历 bean 数据并在您的 jsp 上显示它们的内容。

hello.xml

<bean id="productManager" class="springapp.service.SimpleProductManager">
    <property name="products">
        <list>
            <ref bean="product1"/>
            <ref bean="product2"/>
            <ref bean="product3"/>
        </list>
    </property>
</bean>

<bean id="product1" class="springapp.domain.Product">
    <property name="description" value="Lamp"/>
    <property name="price" value="5.75"/>
</bean>

<bean id="product2" class="springapp.domain.Product">
    <property name="description" value="Table"/>
    <property name="price" value="75.25"/>
</bean>
...

hello.jsp

<c:forEach items="${model.products}" var="prod">
  <c:out value="${prod.description}"/>
  $<c:out value="${prod.price}"/>
</c:forEach>

参考:Spring.io

【讨论】:

  • 我的问题与JSP无关。我正在尝试在 spring 配置文件中使用 ${property} 语法来定义 bean。
猜你喜欢
  • 2010-09-20
  • 2018-09-28
  • 2015-06-13
  • 2011-12-24
  • 2012-03-26
  • 2020-03-21
  • 1970-01-01
  • 2011-09-15
  • 2018-03-01
相关资源
最近更新 更多