从Java.util.properties对象设置属性,以便在运行时将Mule Flow属性占位符



我已经成功地加载了来自App Init的数据库的Mule App属性并将其设置为Mule Flow的属性占位符。此处引用的代码从db

读取mule props

但是,这仅在应用启动期间起作用。我希望能够修改数据库中的属性(我可以),并在不重新启动mule服务器的情况下在运行时反思Mule流。

为了实现这一目标,我使用HTTP侦听器创建了一个新的流程,该侦听器调用了Java类,该类别读取数据库中的属性,并尝试使用PropertySoursoursourcesplaceplaceholderconfigurer类将其设置为Bean Factory。Java类的示例代码看起来像这样。

@Autowired
ConfigurableListableBeanFactory configurableListableBeanFactory;
@Autowired
MyService myService;
public MuleEventContext onCall(MuleEventContext eventContext){
Properties properties = new Properties();
    // get properties from the database
    Map<String,String> propertiesMap = getMuleAppPropertiesFromDB();
    if(null != propertiesMap && !CollectionUtilsIntg.isEmpty(propertiesMap))
        properties.putAll(propertiesMap);
PropertySourcesPlaceholderConfigurer cfg = new PropertySourcesPlaceholderConfigurer();
cfg.setProperties(properties);
cfg.postProcessBeanFactory(configurableListableBeanFactory);
}

此代码可成功地运行,但未能在运行时设置属性。

有人知道这还可以实现吗?

请帮助

我相信,一旦申请完全启动,即它们仅限于应用程序初始化,并且仅在beans创建期间,就可以生命。如果您想在运行时更改属性,则不应使用属性占位符,而应使用其他属性机制,例如创建org.springframework.beans.factory.config.PropertiesFactoryBean

的bean
<?xml version="1.0" encoding="UTF-8"?>
<mule  xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
    <http:listener-config doc:name="HTTP Listener Configuration" host="0.0.0.0" name="HTTP_Listener_Configuration" port="8081"/>
    <spring:beans>
        <spring:bean id="myProps" name="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <spring:property name="properties">
                <bean factory-bean="databasePropertiesProvider" factory-method="getProperties" />       
            </spring:property>
        </spring:bean>
    </spring:beans>
    <flow name="test">
        <http:listener config-ref="HTTP_Listener_Configuration" doc:name="Recieve HTTP request" path="/test"/>
        <logger message="#[app.registry.myProps['testPropertyName']]" />
    </flow>
</mule>

而不是从文件中读取,您可以使用DB加载。然后在您的m子配置中使用这些用作#[app.registry.mypropes ['mykey']]。在此处阅读有关MEL上下文对象的信息。

在上面的示例代码中,我从数据库中注册了myPoros BEAN和加载属性。app.registry是mule中可用的应用程序注册表上下文对象,它使您可以访问Spring Bean。

最新更新