从数据库中读取属性并使用它而不是mule-app.properties

时间:2016-06-27 12:44:19

标签: mule

我已经能够从数据库中的表中读取属性,如此处所述Reading mule config from database

现在,我无法将这些属性应用于流配置,并且还可以通过MuleEventContext的消息将它们作为Java Processor类中的绑定属性进行访问。

更新:下面是我的流程XML代码

<flow name="push-data">
    <poll doc:name="Push Poll">
        <fixed-frequency-scheduler frequency="${push.data.poll.frequency}"  timeUnit="MINUTES" />
    <set-property propertyName="tempFilePath" value="${temp.csv.file.path}" doc:name="Property"/>

    <component class="com.reports.processors.PushDataProcessor" doc:name="PushDataProcessor"/>
    <logger message="worked!!!" level="INFO" doc:name="Logger"/>
    <exception-strategy ref="push-report-data_Catch_Exception_Strategy" doc:name="Reference Exception Strategy"/>
</flow>

我正在尝试设置属性“push.data.poll.frequency”和“temp.csv.file.path”。早些时候,这些属性存在于“mule-app.properties”文件中。

所以,我的问题是,如何将从数据库加载的属性设置为流。请记住,我已经从数据库中加载了属性,如上面的链接所述。我只是希望能够将这些属性设置为流,而不是从mule-app.properties中获取它们。

编辑:添加更多信息, 我正在使用带有@Configuration注释的类。上面链接中描述的类从数据库加载属性。以下是源代码。

@Configuration(name="databasePropertiesProvider")
@Component
public class DatabasePropertiesProvider {

@Autowired(required=true)
private MyService myService;

@Bean
public Properties getProperties() throws Exception {
    Properties properties = new Properties();
    // get properties from the database
    Map<String,String> propertiesMap =    myService.getMuleAppPropertiesFromDB();
    if(null != propertiesMap && !CollectionUtils.isEmpty(propertiesMap))
        properties.putAll(propertiesMap);
    return properties;
}

@Bean
 public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
 }}

但是这个类在应用程序初始化后运行。以前,我在xml配置中配置了PropertySourcesPlaceholderConfigurer,并将factory-bean配置为DatabasePropertiesProvider类。但由于DatabasePropertiesProvider依赖于MyService类,并且由于MyService bean在属性配置之前未在容器中初始化而导致依赖关系未得到解析,因此我不得不对DatabasePropertiesProvider(上面的版本)进行一些更改,以便在应用初始化。

但是现在,问题是我无法访问从数据库加载的那些属性。

更新2:我找到了解决方案。显然我试图在databasePropertiesProvider类中自动装配@Service MyService。自动装配失败并返回null,因此我对databasePropertiesProvider类进行了一些修改,以便在初始化应用程序后运行它。

现在,当我看到它时,我意识到我不需要通过所有服务和存储库层连接到数据库。我将查询执行代码从存储库类移动到databasePropertiesProvider类,现在在初始化期间加载属性,并且流可以获取属性而不进行任何更改。

感谢您的帮助。让我做了很多思考。

此致 佐勒菲卡尔

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。显然我试图在databasePropertiesProvider类中自动装配@Service MyService。自动装配失败并返回null,因此我对databasePropertiesProvider类进行了一些修改,以便在初始化应用程序后运行它。

现在,当我看到它时,我意识到我不需要通过所有服务和存储库层连接到数据库。我将查询执行代码从存储库类移动到databasePropertiesProvider类,现在在初始化期间加载属性,并且流可以获取属性而不进行任何更改。

整个代码看起来像这样 XML配置: -

<bean class="net.intigral.reports.provider.properties.DatabasePropertiesProvider" id="databasePropertiesProvider">
    <property name="entityManager" ref="entityManager" />
 </bean>

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="properties">
        <bean factory-bean="databasePropertiesProvider" factory-method="getProperties" />
    </property>
</bean>  

Java代码: -

public class DatabasePropertiesProvider {

EntityManager entityManager;

public Properties getProperties() throws Exception {
    Properties properties = new Properties();

    // get properties from the database
    Map<String,String> propertiesMap = getMuleAppPropertiesFromDB();
    if(null != propertiesMap && !CollectionUtilsIntg.isEmpty(propertiesMap))
        properties.putAll(propertiesMap);
    return properties;
}

public EntityManager getEntityManager() {
    return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@SuppressWarnings("unchecked")
private Map<String,String> getMuleAppPropertiesFromDB() {
    Map<String,String> collect = null;
    String query = "select key, value from MuleAppProps muleAppProps";
     List<Object[]> results = entityManager.createQuery(query).getResultList();
     if (CollectionUtilsIntg.isNotEmpty(results)) {
            collect = results.stream().collect(Collectors.toMap(o -> (String)o[0], o -> (String)o[1]));
     }
     return collect;
}}

现在,我可以像以前从FLOW中的mule-app.properties加载一样加载属性。

答案 1 :(得分:0)

Let your db contains following properties values with key/value pair as below :-
enter image description here

如下所示,您可以参考数据库中的读取值: -

<spring:beans>
    <spring:bean id="dataSource" name="myCon" class="org.enhydra.jdbc.standard.StandardDataSource">
        <spring:property name="url" value="jdbc:sqlserver://YourIpAddress\\SQLEXPRESS:1433;databaseName=YourDB;user=sa;password=yourDBPassword" />
        <spring:property name="driverName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    </spring:bean>

    <!-- Required to connect to datasource -->
    <spring:bean name="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <spring:property name="properties" ref="CommonsConfigurationFactoryBean" />
    </spring:bean>

    <spring:bean name="CommonsConfigurationFactoryBean"
        class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
        <spring:constructor-arg ref="DatabaseConfiguration" />
    </spring:bean>

    <spring:bean name="DatabaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
        <spring:constructor-arg type="javax.sql.DataSource" ref="dataSource" />
        <spring:constructor-arg index="1" value="YourTableName" />
        <spring:constructor-arg index="2" value="Key" />
        <spring:constructor-arg index="3" value="Value" />
    </spring:bean>

</spring:beans>

<db:generic-config name="Database_Configuration" dataSource-ref="dataSource" doc:name="Generic Database Configuration" />

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />

<flow name="firstflow" processingStrategy="synchronous">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" />
    <set-payload value="File name ${file.name} File path ${file.path}" doc:name="Set Payload" />
</flow>

您需要在类路径中添加commons-configuration.jarspring.jarspring-modules-jakarta-commons.jar

如果要访问Java类中的属性值,可以使用Spring bean init-method中的Spring属性注入它。 参考: - http://www.codeproject.com/Articles/28893/Loading-Application-Properties-from-a-Database