Announcement Announcement Module
Collapse
No announcement yet.
Use of PropertiesFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use of PropertiesFactoryBean

    I am using a PropertiesFactoryBean to read properties from a properties file using the following configuration:

    <bean id="appConfig" class="org.springframework.beans.factory.config.Pr opertiesFactoryBean">
    <property name="location">
    <value>classpath:config/applicationConfig.properties</value>
    </property>
    </bean>

    Now i want the appConfig properties bean to be available to whatever class that needs to look at these properties, BUT i dont want to wire it into every class that uses it and I dont want to define an explicit property in all the classes that use it. Is there a way to wire this into whatever class that needs it without explicitly wiring this ?

    Reply is appreciated

  • #2
    BTW, you can use a PropertyPlaceholderConfigurer to access specific properties of that loaded prop file. That allows more focused reuse.

    Code:
    <bean id="propertyConfigurer"       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="properties"><ref bean="appConfig"></ref></property>        
        </bean>

    Comment


    • #3
      Thanks jbetancourt

      I will surely do that

      My question will still stand with the new bean...

      Now i want the propertyConfigurer bean to be available to whatever class that needs to look at these properties, BUT i dont want to wire it into every class that uses it and I dont want to define an explicit property in all the classes that use it. Is there a way to wire this into whatever class that needs it without explicitly wiring this ?

      Comment


      • #4
        Well, you can use child/parent beans. The parent can have that property reference defined, and the child beans (which don't have to be java class subclasses, btw) will have access to them. The parent can be an abstract bean.

        Of course, now each child bean definition will have to have a parent attribute.

        Comment


        • #5
          Thats the solution I had thought of but wanted to know if there is a way using advices to do that or of there is any special way usng the applicationContext. Just want to make sure before implementing a solution.

          Comment


          • #6
            Well, there is autowiring available.

            Comment


            • #7
              In that case too, I would have to define a property and get set methods in each bean which will be using the properties bean. To avoid this I might have to have a base class with the proprty and getters and setters and all other classes extending this class. Probably the parent child bean solution might be the best.

              Correct me if I am wrong

              Thanks

              Comment


              • #8
                Well I tested the parent bean and even for that I have to define property in each child bean.

                The only way is to subclass from a base class which has this property and getters and setters. All beans needing the property will subclass this. That way I dont have to write getters and setters in each bean. Then I am explicitly wiring the property into each bean whcih uses it. I will further look into autowiring to see if I can do away with explicit wiring

                Comment

                Working...
                X