问题
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