美文网首页
解惑:#{@systemProperties}

解惑:#{@systemProperties}

作者: 一个理想主义的大兵 | 来源:发表于2018-07-27 16:13 被阅读0次

有些的spring项目,核心的xml文件中,都有如下一段配置,其中的#{@systemProperties}让我困惑很久:

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
            <props>
                <prop key="moaLogPath">/home/logs/moa-service/dispatch-video-service</prop>
                <prop key="moaPort">13601</prop>
                <prop key="protocol">MIX</prop>
                <prop key="runMode">online</prop>
                <prop key="momo.log.name">./log4m_video.properties</prop>
                <prop key="momo.alarm.appname">dispatch-video-service</prop>
                <prop key="redisClientTimeout">500</prop>
                <prop key="warmupSeconds">60</prop>
            </props>
        </property>
    </bean>

这个配置的通俗理解:在bean的生命周期的初始化阶段,通过SpEL表达式,引用systemProperties的bean,通过MethodInvokingFactoryBean调用引用bean的putAll方法,将所配置的属性(moaLogPath、moaPort…等),注入到System的Properties中。

Spring Expression Language (SpEL)

#{@systemProperties}这是一种spring表达式,SpEL表达式支持使用“@”符号来引用Bean。

详见官方文档

ClassPathXmlApplicationContext 实现默认会把“System.getProperties()”注册为“systemProperties”Bean,因此我们使用 “@systemProperties”来引用该Bean。

MethodInvokingFactoryBean

MethodInvokingFactoryBean是一种bean,实现了FactoryBean<Object>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean接口。

作用是调用targetObject的targetMethod,可以有返回值(比如得到一个bean),也可以没有返回值(比如某些赋值操作)。

调用的时机是依赖于bean生命周期中的afterPropertiesSet(),在此方法中完成调用。

官方文档:

This class depends on afterPropertiesSet() being called once all properties have been set, as per the InitializingBean contract.

相关文章

网友评论

      本文标题:解惑:#{@systemProperties}

      本文链接:https://www.haomeiwen.com/subject/ctjgmftx.html