Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a PropertyPlaceholderConfigurer-like class for use with Spring that accepts XML?

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?

like image 783
GaryF Avatar asked Jan 26 '09 13:01

GaryF


People also ask

What can I use instead of PropertyPlaceholderConfigurer?

Just use: final var configurer = new PropertySourcesPlaceholderConfigurer(); configurer. setProperties(properties);

What is the use of PropertyPlaceholderConfigurer in spring?

Class PropertyPlaceholderConfigurer. A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

How do you externalize a constants from a Spring XML configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.

What are the uses of XML configuration file in spring application?

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.


1 Answers

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">
<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...

like image 176
toolkit Avatar answered Oct 11 '22 17:10

toolkit