How to use a properties file in Spring beans XML file?

久未见 提交于 2019-12-12 02:33:44

问题


I have some properties files that I'd like to make available in the Spring XML config files. For example, in hello.xml:

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

In Java code:

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());

In hello.properties:

foo.color = blue

How do I make the properties files available to the Spring object definitions?

Update

I'm porting some old Spring code. (Version 2.5) It looked sort of like this:

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);

This code was marked deprecated, and I was having other problems, so I tried to port it to the new way.


回答1:


You Could read the more.properties in java code with the help of PropertyPlaceholderConfigurer. Not sure why you would really need to read as File Object.

In hello.xml file

<!-- 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>

In hello.properties file

 foo.color=blue

In more.properties file

foo.shape=square

In Java Code

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



回答2:


In case you also want to loop through the bean data and display their contents on your 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>

Reference: Spring.io



来源:https://stackoverflow.com/questions/43710016/how-to-use-a-properties-file-in-spring-beans-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!