views:

1848

answers:

4

Hello,

I need to store some configuration parameters for a web application that uses spring framework.

Typically I'll use a configurationfile.properties file but i wonder if i can store that values in the applicationContext.xml file.

One workaround could be to create a JavaBean class to store the values, and build that class using spring, something like this:

<bean id="configurationBean" class="mypackage.someClass">
 <property name="confValue1">
   <value>myValue1</value>
 </property>
 ....
</bean>

But i would like to know if is there a way to store those parameters without the needing to create that class.

Thanks in advance.

A: 

Spring has builtin support for specifying properties within the application context XML. See section 3.3.2.4 of the Spring Reference docs.

Mark
Thank you for the response.But i want to avoid to create one POJO object to store the properties. I Read the link you provided, i can specify properties but always inside a <bean> definition element, so that links the bean with a POJO class.
HyLian
So you want the actual <bean> to be an instance of java.util.Properties?
Mark
That is what's i was searching for. Thank you :)
HyLian
+1  A: 

I think you'll get the best results using Spring's PropertyPlaceholderConfigurer which allows you to map values from a regular .properties file against properties defined on your beans.

http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

The example shows how to set the JDBC connection properties directly on an instance of javax.sql.DataSource, eliminating the need for an intermediate "configuration bean."

cliff.meyers
Thanks for answering.Your solution doesn't seem bad, but i would like not to create a separate .properties file but to store the properties values only in the appContext.xml file, and access them in Java code.
HyLian
Then you want to refer to link Mark provided. It shows how you can declare an instance of java.util.Properties in XML: <property name="adminEmails"> <props> <prop key="administrator">[email protected]</prop> <prop key="support">[email protected]</prop> <prop key="development">[email protected]</prop> </props> </property>
cliff.meyers
@HyLian you can't just put arbitrary properties (that look like a .properties file) in an XML file. You either have to follow the XML structure (as @brd6644 mentioned above) or put it in a separate .properties file, as he mentioned in his post.
Alex Beardsley
+1  A: 

This should work with the following syntax.

<bean id="props" class="java.util.Properties" >
    <constructor-arg>
        <props>
            <prop key="myKey">myValue</prop>
            <prop ...>
        </props>
    </constructor-arg>
</bean>

You are taking advantage of the fact that java.util.Properties has a copy constructor that takes a Properties object.

I do this for a HashSet which also has a copy constructor (as do HashMaps and ArrayLists) and it works perfectly.

Darren Greaves
A: 

I think that the best solution that fits my requirements is to use a java.util.Properties instance as a Spring Bean.

Thank you all.

HyLian