Results 1 to 8 of 8

Thread: Use of PropertiesFactoryBean

  1. #1
    Join Date
    Jul 2005
    Posts
    17

    Default 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. #2
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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>

  3. #3
    Join Date
    Jul 2005
    Posts
    17

    Default

    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 ?

  4. #4
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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.

  5. #5
    Join Date
    Jul 2005
    Posts
    17

    Default

    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.

  6. #6
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    Well, there is autowiring available.

  7. #7
    Join Date
    Jul 2005
    Posts
    17

    Default

    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

  8. #8
    Join Date
    Jul 2005
    Posts
    17

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •