简体   繁体   中英

How to use a custom xml file to load variables to my spring.xml using PropertyPlaceholderConfigurer?

I´m trying to use a custom xml as my properties file so I can define some variables on the spring.xml file.

I searched for similar question, but the xml file had to follow a standard to be use.

Normally I would load the .properties files like this:

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:///PATH_TO_FILE\file.properties"/>
</bean>

So then I can inject properties to my spring.xml in variables in the form of ${variable} .

My custom xml looks like this:

<configAttributes>
<attributes>
    <attribute>
        <name>NameOftheVariable</name>
        <description>Text</description>
        <value>valueIWantoToInject</value>
    </attribute>
<attributes>

I´m using spring-beans 2.5 and Java EE 6.

Thanks.

Read xml file using any parser, I assume you are already doing it as you mentioned in your comments.

so now you have a client bean inside your spring configuration which needs the property to be injected from xml parsed values.

So, Just load that bean in spring as

BeanClass bean = `context.getBean(clientBeanid);

then set the parsed values from xml file to bean as below

bean.setName(parsedxmlname);
bean.setName(parsedxmldescription);

and so on.

It's a two step solution

  1. Overriding PropertyPlaceholderConfigurer You have to override the resolvePlaceholder() where in the logic will be inserted to return the expected result

  2. Parsing the xml The parser should help to read the xml to the required format and supply it to your resolvePlaceholder()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM