Results 1 to 5 of 5

Thread: Programmatic access to properties defined for the PropertyPlaceholderConfigurer?

  1. #1

    Question Programmatic access to properties defined for the PropertyPlaceholderConfigurer?

    Hi,

    There is no mention in the documentation but is there any programmatic access (ie API / SPI) to the properties defined for the PropertyPlaceholderConfigurer?

    I would like to create a smart instantiation of javax.swing.Action. All action 'properties' (like NAME , SMALL_ICON...) would be looked in the available properties or messages and set up if present.

    Regards,

    Bertrand Dechoux

  2. #2
    Join Date
    Aug 2011
    Location
    Cape Town
    Posts
    4

    Default Programmatic access to properties defined for the PropertyPlaceholderConfigurer?

    Hi bertrand

    If I understand the question then the answer is trivial - if not please accept my apologies!

    Long before Spring was using PropertyPlaceholderConfigurer Java used properties files directly.

    If we have some application context as follows:
    Code:
    	
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyConfigurer">
    	<property name="location">
    		<value>file:C:/CONFIG/settings.properties</value>
    	</property>
    </bean>
    ...
    <bean class="fe.bl.config.Foo" id="foo">
    	<constructor-arg value="${SMALL_ICON}" index="0" type="java.lang.String"/>
    ...
    Then the following Java code can read the property "SMALL_ICON" directly:
    Code:
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("C:/CONFIG/settings.properties"));
    } catch (IOException e) {
    }
    String smallIcon = properties.getProperty("SMALL_ICON");
    }

  3. #3

    Default

    That's the spirit but I am not looking to get rid of Spring really.

    Ideally, I would want to have the property place holder and be able to grammatically get a property from it.
    That way, I could share the configuration and not alienate other developers.

    I have been looking at its API and it does not seem like there is an easy way to do it.
    For the moment, I am using MessageSource... but the semantic is not quite right....

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I have been looking at its API and it does not seem like there is an easy way to do it.
    Well look closer, actually there is.. Configure a PropertiesFactoryBean and wire that to the PropertyPlaceHolderConfigurer.. That way you have a properties object which you can use in your software.

    Code:
    	
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyConfigurer">
    	<property name="properties" ref="props" />
    </bean>
    
    <bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="location" value="file:C:/CONFIG/settings.properties"/>
    </bean>
    Nowyou can simply inject the properties object into the bean you need it in and retrieve the properties (while it still uses the properties to do replacement in your configuration files).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Thumbs up

    Indeed. Thanks a lot, it is exactly what I was looking for.

Posting Permissions

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