简体   繁体   English

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

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

I have defined a custom PropertyPlaceholderConfigurer which makes a REST call to get the properties that are used to resolve the placeholders. 我定义了一个自定义的PropertyPlaceholderConfigurer,它进行REST调用以获取用于解析占位符的属性。

However the REST call URL is injected by Spring. 但是,REST调用URL是由Spring注入的。 And apparently this injection is called after the PropertyPlaceholderConfigurer is done. 显然在完成PropertyPlaceholderConfigurer 之后将调用此注入。 This is causing an exception, since the URL is null at the time the PlaceholderConfigurer needs it. 这会导致异常,因为在PlaceholderConfigurer需要URL时该URL为null。

I need the chicken to come before the egg. 我需要鸡肉先于鸡蛋。 Is there any way to get the injectors to run before the PlaceholderConfigurer? 有什么方法可以使PlaceholderConfigurer 之前的喷射器运行? If not, is there some way for the PlaceholderConfigurer to get a sneak preview of the coming injections? 如果没有,PlaceholderConfigurer是否可以通过某种方式来预览即将到来的注射?

We run a similar configuration, in our case we have the database credentials stored in a local config file, all other properties are stored in the database. 我们运行类似的配置,在这种情况下,我们将数据库凭据存储在本地配置文件中,所有其他属性都存储在数据库中。

<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>

In our case, the DatabasePropertyPlaceholderConfigurer needs to access a Spring Bean (the DataSource), so we use the BeanFactory in the override 'mergeProperties()' method to retrieve that DataSource. 在我们的例子中,DatabasePropertyPlaceholderConfigurer需要访问一个Spring Bean(数据源),因此我们在重写'mergeProperties()'方法中使用BeanFactory来检索该数据源。 In your case, the configuration is a lot simpler, since you need a simple URL configuration value. 就您而言,配置要简单得多,因为您需要一个简单的URL配置值。

The example below might do the trick: (please notice that the two configurers use a different prefix/suffix: $[] instead of ${}) 下面的示例可以解决这个问题:(请注意,两个配置器使用不同的前缀/后缀:$ []代替$ {})

<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.

相关问题 如何在OutOfMemoryError _after_上进行堆转储时重新启动JVM? - How can I restart JVM on OutOfMemoryError _after_ making a heap dump? 运行后如何在我的spring basicdatasource中添加新的数据库连接详细信息? - How can i add new database connection details to my spring basicdatasource after run time? Spring单例初始化完成后如何运行方法? - How can I run a method after the Spring singleton initialization is complete? 如何离线运行我的Spring应用程序? - How can I run my Spring apps offline? 如何获得PropertyPlaceHolderConfigurer中所有属性的列表? - How can I get a list of all properties in PropertyPlaceHolderConfigurer? PropertyPlaceholderConfigurer 和 PropertySourcesPlaceholderConfigurer 运行方式不同 - PropertyPlaceholderConfigurer and PropertySourcesPlaceholderConfigurer run differently 如何使用PropertyPlaceholderConfigurer使用自定义xml文件将变量加载到我的spring.xml中? - How to use a custom xml file to load variables to my spring.xml using PropertyPlaceholderConfigurer? 如何让我的程序正确运行未来值? - How can I get my program to run the future values correctly? 如何使程序在Java窗口中运行? - How can I get my program to run within a window in Java? 我怎样才能让它运行? - How can I get this to run?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM