0

I have service.properties and service-properties.xml in mule based project. How can i define that service.properties should load before service-properties.xml. Because i am using the properties of service.properties in service-properties.xml. because i have this code in service-properties.xml file:

    <context:property-placeholder
        location="classpath:mule/service.properties"
        properties-ref="propertiesHolder" />
    <context:annotation-config />   
   <spring:bean id="propertiesHolder"
        class="MyClass">
        <spring:property name="dataSource" ref="dataSource" />
    </spring:bean>
    <spring:bean id="dataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <spring:property name="driverClassName value="${db.driverClassName}" />
        <spring:property name="url" value="${db.url}" />
        <spring:property name="username" value="${db.username}" />
        <spring:property name="password" value="${db.password}" />
    </spring:bean>

My project is build successfully but when i run project it can not resolve db.driverClassName, db.url, ... and db.password from service.properties but i have them in service.properties file.

Fariba
  • 693
  • 1
  • 12
  • 27

3 Answers3

0

If you are loading the properties files and config files within the same context using context:property-placeholder then Mule/Spring will take of that for you and there shoould be no problems:

https://docs.mulesoft.com/mule-user-guide/v/3.7/configuring-properties

Ryan Carter
  • 11,441
  • 2
  • 20
  • 27
  • my project is build. when i run my project i have db.url=${property.url} in my an my xml file. the value of property.url is in the my .properties file. but it can not read property.url from service.properties because i think it is not loaded. – Fariba Oct 07 '15 at 16:28
0

I think your application is failing to resolve the properties because you included both a location attribute and a properties-ref attribute in your property-placeholder configuration. The properties-ref attribute is intended to replace the location attribute, in order to provide the values of the properties from a java.util.Properties object.

Try this:

<context:property-placeholder location="classpath:mule/conversion/service.properties" />

You can then simply refer to the properties in your dataSource bean.

The reason there is no explicit reference connecting the bean using the properties (dataSource) and the bean that loads the properties is that the <context:property-placeholder /> element defines a BeanFactoryPostProcessor. This is a special kind of bean that can modify the definition of other beans before they are instantiated. It searches for bean properties using the ${propertyName} syntax and replaces them before Spring tries to instantiate them.

UPDATED: How to load properties from database, configured by properties

If your goal is to load your configuration properties from a database, and to configure the connection to that database with another properties file, check out this StackOverflow question.

Community
  • 1
  • 1
Ryan Hoegg
  • 2,415
  • 2
  • 14
  • 15
  • @Rayan Hoegg I need to read properties from properties file and also i need to read properties which is saved in database. when i seperate them in 2 property plase holder also i have the same problem – Fariba Oct 08 '15 at 21:03
0

I could read properties from file and from database by this configuration:

   <spring:bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <spring:property name="locations">
            <spring:value>classpath:mule/service.properties</spring:value>
        </spring:property>
        <spring:property name="properties">
            <spring:value>propertiesFromDataSource</spring:value>
        </spring:property>
    </spring:bean>
Fariba
  • 693
  • 1
  • 12
  • 27