views:

1690

answers:

2

Spring has a very handy convenience class called PropertyPlaceholderConfigurer, which takes a standard .properties file and injects values from it into your bean.xml config.

Does anyone know of a class which does exactly the same thing, and integrates with Spring in the same way, but accepts XML files for the config. Specifically, I'm thinking of Apache digester-style config files. It would be easy enough to do this, I'm just wondering if anyone already has.

Suggestions?

+5  A: 

I just tested this, and it should just work.

PropertiesPlaceholderConfigurer contains a setPropertiesPersister method, so you can use your own subclass of PropertiesPersister. The default PropertiesPersister already supports properties in XML format.

Just to show you the fully working code:

JUnit 4.4 test case:

package org.nkl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = { "classpath:/org/nkl/test-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class PropertyTest {

    @Autowired
    private Bean bean;

    @Test
    public void testPropertyPlaceholderConfigurer() {
        assertNotNull(bean);
        assertEquals("fred", bean.getName());
    }
}

The spring config file test-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
  <context:property-placeholder 
      location="classpath:/org/nkl/properties.xml" />
  <bean id="bean" class="org.nkl.Bean">
    <property name="name" value="${org.nkl.name}" />
  </bean>
</beans>

The XML properties file properties.xml - see here for description of usage.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
<properties>
  <entry key="org.nkl.name">fred</entry>
</properties>

And finally the bean:

package org.nkl;

public class Bean {
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

Hope this helps...

toolkit
That's definitely handy to know, and will be a handy back-up. I guess it's easy enough to override PropertiesPersister to implement Apache Digester style parsing, instead of the standard properties xml format.
GaryF
+3  A: 

Found out that Spring Modules provide integration between Spring and Commons Configuration, which has a hierarchial XML configuration style. This ties straight into PropertyPlaceholderConfigurer, which is exactly what I wanted.

GaryF
Nice one GaryF - +1 :-)
toolkit