0

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.

2 Answers2

0

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.

Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
  • Thanks @karibasappa-g-c I´m am aware of the standard but the company I´m working with does not want to use the .properties file, instead use the custom xml file. – Jonathan Martinez Jul 22 '15 at 20:56
  • oh okie...then check with them which parser should you use as there are many xml parsers to read xml file... – Karibasappa G C Jul 22 '15 at 20:58
  • They are using `javax.xml` to parse the xml file. But after they parse and save the value of the attibute I don´t see how can I inject it in the spring.xml in the placeholder of a variable – Jonathan Martinez Jul 22 '15 at 21:08
  • After parsing, where are the keeping those parsed value ? – Karibasappa G C Jul 22 '15 at 21:14
  • They store all the values from the xml file in a class which sets all the variables and constants of the application. – Jonathan Martinez Jul 22 '15 at 21:30
  • then why do you need to inject again in spring xml file, you can use it from your application class right – Karibasappa G C Jul 22 '15 at 21:32
  • Let me give you an example of what I´m trying to do. I have my custom xml file, I parse it , and save the value of that variable, this variable holds the URL of a web service. In my spring.xml I have bean for my client which connects to the web service, I want to be able to replace the URL value of the client bean which the value I get from my custom xml – Jonathan Martinez Jul 22 '15 at 21:42
  • check the updated answer, i think it should help you !! – Karibasappa G C Jul 23 '15 at 20:31
  • Just to be clear, the `bean.setName` would be name of my variable in my spring.xml file? – Jonathan Martinez Jul 23 '15 at 21:01
  • yes correct, in fact corresponding java class should also be there right, so get the setter from there... – Karibasappa G C Jul 23 '15 at 21:23
0

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()

Mudassar
  • 3,135
  • 17
  • 22