Set System Property Using Spring Configuration File

Configuration :
Spring 2.5, Junit 4, Log4j
The location of the log4j file is specified from the system property

${log.location} 

At run time, the system property is set with the -D java option. All right.

Problem / what I need:
In unit test, the system property is not set and the file location is not allowed.
The application uses Spring, I would just like to configure Spring to set the system property.

Additional Information:
Requirement for configuration only. Unable to enter new Java code or entries in the IDE. Ideally, one of the Spring property configuration implementations could handle this - I just couldn't find the right combination.

This idea is close, but you need to add Java code:
Spring SystemPropertyInitializingBean

Any help there? Any ideas are welcome.

+51
java spring junit log4j
Jul 26 '10 at 23:33
source share
4 answers

You can achieve this with a combination of MethodInvokingFactoryBeans

Create an internal bean that accesses System.getProperties and an external bean that calls putAll on the properties obtained by the internal bean:

 <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <!-- System.getProperties() --> <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"> <!-- The new Properties --> <util:properties> <prop key="my.key">myvalue</prop> <prop key="my.key2">myvalue2</prop> <prop key="my.key3">myvalue3</prop> </util:properties> </property> </bean> 

(Of course you can only use one bean and the target System.setProperties (), but then you would replace the existing properties, which is not a good idea.

Anyway, here is my little testing method:

 public static void main(final String[] args) { new ClassPathXmlApplicationContext("classpath:beans.xml"); System.out.println("my.key: "+System.getProperty("my.key")); System.out.println("my.key2: "+System.getProperty("my.key2")); System.out.println("my.key3: "+System.getProperty("my.key3")); // to test that we're not overwriting existing properties System.out.println("java.io.tmpdir: "+System.getProperty("java.io.tmpdir")); } 

And here is the conclusion:

 my.key: myvalue my.key2: myvalue2 my.key3: myvalue3 java.io.tmpdir: C:\DOKUME~1\SEANFL~1\LOKALE~1\Temp\ 
+54
Jul 27 '10 at 5:26
source share

In the comments for Spring 3, for example, there was a request on how to do this.

 <bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" value="#{@systemProperties}" /> <property name="targetMethod" value="putAll" /> <property name="arguments"> <!-- The new Properties --> <util:properties> <prop key="java.security.auth.login.config">/super/secret/jaas.conf</prop> </util:properties> </property> </bean> 
+87
Sep 12 2018-11-12T00:
source share

Spring Batch has a SystemPropertyInitializer class that can be used to more easily set a system property, for example. to get JBoss to register to use slf4j (with Spring JPA):

 <bean id="setupJBossLoggingProperty" class="org.springframework.batch.support.SystemPropertyInitializer" p:keyName="org.jboss.logging.provider" p:defaultValue="slf4j"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="setupJBossLoggingProperty" 

Be sure to add the "depends on" attribute to force the system property to be set.

+10
Dec 03 '12 at 19:44
source share

For a finer approach, try:

 <beans ... xmlns:p="http://www.springframework.org/schema/p" ... <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" p:targetObject="#{@systemProperties}" p:targetMethod="setProperty" p:arguments="#{{'org.jboss.logging.provider','slf4j'}}"/> 
+3
May 28 '15 at 9:40
source share



All Articles