繁体   English   中英

我怎样才能让Spring运行我的自定义PropertyPlaceholderConfigurer _after_注入器运行

[英]How can I get Spring to run my custom PropertyPlaceholderConfigurer _after_ the injectors run

我定义了一个自定义的PropertyPlaceholderConfigurer,它进行REST调用以获取用于解析占位符的属性。

但是,REST调用URL是由Spring注入的。 显然在完成PropertyPlaceholderConfigurer 之后将调用此注入。 这会导致异常,因为在PlaceholderConfigurer需要URL时该URL为null。

我需要鸡肉先于鸡蛋。 有什么方法可以使PlaceholderConfigurer 之前的喷射器运行? 如果没有,PlaceholderConfigurer是否可以通过某种方式来预览即将到来的注射?

我们运行类似的配置,在这种情况下,我们将数据库凭据存储在本地配置文件中,所有其他属性都存储在数据库中。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="locations">
        <list>
            <value>classpath:app.properties</value>
        </list>
    </property>
    <property name="placeholderPrefix" value="$["/>
    <property name="placeholderSuffix" value="]"/>
</bean>

<bean id="propertyConfigurer" class="com.acme.util.DatabasePropertyPlaceholderConfigurer">
    <property name="order" value="2"/>
    <property name="dataSourceName" value="dataSource"/>
</bean>

在我们的例子中,DatabasePropertyPlaceholderConfigurer需要访问一个Spring Bean(数据源),因此我们在重写'mergeProperties()'方法中使用BeanFactory来检索该数据源。 就您而言,配置要简单得多,因为您需要一个简单的URL配置值。

下面的示例可以解决这个问题:(请注意,两个配置器使用不同的前缀/后缀:$ []代替$ {})

<bean id="propertyConfigurer" class="com.acme.util.RESTPropertyPlaceholderConfigurer">
    <property name="order" value="2"/>
    <property name="url" value="$[config.url]"/>
</bean>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM