Search code examples
javaspringpropertieslog4jproperties-file

Load properties from PropertyPlaceholderConfigurer to System properties


I'm using spring 4.1.0.RELEASE and log4j 1.2.14. I use the next lines to load and resolve properties:

<bean class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer" id="propsResolver">
    <constructor-arg>
        <bean class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
            <property name="config">
                <bean class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
                    <property name="algorithm" value="PBEWithMD5AndDES" />
                    <property name="passwordEnvName" value="ENC_KEY" />
                </bean>
            </property>
        </bean>
    </constructor-arg>
    <property name="location" value="${properties.file}"/>
    <property name="searchSystemEnvironment" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>

In log4j configuration I have:

<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="/path/to/myfile-${instance}.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%-5p] %d %c - %m%n"/>
    </layout>
</appender>

In ${properties.file} I have 'instance' property. So I run several instances of my code and want each of them to log into separate file. But I see that even when I start the 1st one it logs into myfile-.log without instance property value in file name. This happens because properties loaded by EncryptablePropertyPlaceholderConfigurer are not loaded to System properties. How can I change it? What should I do to make placeholder configurer load properties into system. Thank you


Solution

  • <bean id="propertyLoader" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                <property name="targetClass" value="java.lang.System"/>
                <property name="targetMethod" value="getProperties"/>
            </bean>
        </property>
        <property name="targetMethod" value="putAll"/>
        <property name="arguments">
            <util:properties location="${myfile.properties}">
            </util:properties>
        </property>
    </bean>