2017-04-30 86 views
0

我有一些属性文件,我想在Spring XML配置文件中提供。例如,在hello.xml如何在Spring beans 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对象定义?

更新

我移植一些旧的春天代码。 (版本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); 

这段代码被标记为过时,我有其他的问题,所以我想将它移植到了新的途径。

+0

也许'PropertyPlaceHolderConfigurer'? http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer –

+0

这显示了如何在XML文件中硬编码属性的路径,但如果我在Java代码中有一些'File'对象? –

回答

-1

如果您还想循环遍历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

+0

我的问题与JSP无关。我试图在spring配置文件中使用'$ {property}'语法来定义bean。 –

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

File对象是因为我正在处理遗留代码。 –

+0

好的。你是否试图看到下面的东西,而不使用弹簧? https://www.mkyong.com/java/java-properties-file-examples/ 如果您需要将这些属性加载到Foo对象,则可以自动装入它并设置值。 – Jajikanth

+0

我正在移植较旧的Spring代码。我在“更新”下添加了它。 –

相关问题