使用Spring配置文件设置系统属性

Ste*_*eve 57 java junit spring log4j

配置:
Spring 2.5,Junit 4,Log4j
log4j文件位置是从系统属性指定的

${log.location}
Run Code Online (Sandbox Code Playgroud)

在运行时,系统属性使用-D java选项设置.一切都很好.

问题/我需要什么:
在单元测试时,系统属性未设置,文件位置未解析.
App使用Spring,想简单地配置Spring来设置系统属性.

更多信息:
要求仅适用于配置.无法将新的Java代码或条目引入IDE.理想情况下,Spring的一个属性配置实现可以解决这个问题 - 我只是无法找到正确的组合.

这个想法很接近,但需要添加Java代码:
Spring SystemPropertyInitializingBean

有帮助吗?任何想法都表示赞赏.

Pat*_*ick 92

有关如何执行此操作的Spring 3示例的注释中有一个请求.

<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>
Run Code Online (Sandbox Code Playgroud)

  • @ziggy如果我有一个bean`exampleBean`需要设置那些系统属性,那么我做``bean id ="exampleBean"class ="ExampleBean"depends-on ="systemPrereqs"/>`以确保它们是在创建exampleBean之前设置. (2认同)

Sea*_*oyd 55

您可以通过两个MethodInvokingFactoryBeans的组合来实现这一点

创建一个访问System.getProperties的内部bean和一个外部bean,它在内部bean获取的属性上调用putAll:

<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>
Run Code Online (Sandbox Code Playgroud)

(当然,您可以只使用一个bean并将目标定为System.setProperties(),但是您将替换现有属性,这不是一个好主意.

无论如何,这是我的小测试方法:

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"));
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

my.key: myvalue
my.key2: myvalue2
my.key3: myvalue3
java.io.tmpdir: C:\DOKUME~1\SEANFL~1\LOKALE~1\Temp\
Run Code Online (Sandbox Code Playgroud)

  • Spring 3通过消除第二个MethodInvokingFactoryBean的需要简化了这一过程.使用SpEL,目标对象行变为<property name ="targetObject"value ="#{@ systemProperties}"/> (7认同)

pau*_*lcm 11

Spring Batch有一个SystemPropertyInitializer类,可用于稍微简洁地设置系统属性,例如强制JBoss日志记录使用slf4j(使用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"
Run Code Online (Sandbox Code Playgroud)

请记住添加"依赖"属性以强制首先设置系统属性.