从数据库读取属性并使用它代替mule-app.properties [英] Read properties from database and use it instead of mule-app.properties

查看:81
本文介绍了从数据库读取属性并使用它代替mule-app.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够从数据库中的表中读取属性,如此处所述从数据库中读取ule子配置

I have been able to read the properties from a table in the database as it was described here Reading mule config from database

现在,我无法将这些属性应用于流配置,也无法通过MuleEventContext的消息将它们作为Java处理器类中的绑定属性进行访问.

Now, I am not able to apply these properties to the flow configs and also access them as out bound properties in the Java Processor classes through the MuleEventContext's message.

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

Update: below is my flow XML code

<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"文件中.

I am trying to set the properties "push.data.poll.frequency" and "temp.csv.file.path". Earlier, these properties existed in the "mule-app.properties" file.

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

So, My question is, How do I set the properties loaded from the database to the flow. Please keep in mind that I have already loaded the properties from the database as described in the link above. I just want to be able to set these properties to the flow rather than taking them from the mule-app.properties.

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

To add some more information, I am using a class with @Configuration annotation. The class as described in the link above, loads the properties from the database. Below is the source code.

@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配置中使用工厂bean作为DatabasePropertiesProvider类配置了PropertySourcesPlaceholderConfigurer.但是由于DatabasePropertiesProvider对MyService类具有依赖关系,并且由于MyService bean在属性配置之前未在容器中初始化,因此无法解决该依赖关系,因此我必须对DatabasePropertiesProvider(以上版本)进行一些更改,以使其在应用初始化.

But this class runs after the app is initialized. Previously, I had configured the PropertySourcesPlaceholderConfigurer in the xml config with the factory-bean as the DatabasePropertiesProvider class. But since DatabasePropertiesProvider has a dependency on MyService class, and the dependency was not getting resolved due to MyService bean not initializing in the container before the property config, I had to make some changes to DatabasePropertiesProvider(the version above) so that this runs after the app initialization.

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

But now, the problem is that I am unable to access those properties that are loaded from the database.

更新2:我找到了解决方案.显然我在尝试自动连接databasePropertiesProvider类中的@Service MyService.自动装配失败,出现null信息,这是因为我对databasePropertiesProvider类进行了更多修改,以便在应用程序初始化后运行.

UPDATE 2: I found a solution. Apparently I was trying to autowire the @Service MyService in the databasePropertiesProvider class. The autowiring was failing with null due to which I made some more modifications to the databasePropertiesProvider class so that It runs after the app is initialized.

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

Now when I look at it, I realized that I dont need to connect to the database through all the service and repository layers. I moved the query execution code from the repository class to the databasePropertiesProvider class and now the properties are loaded during initialization time and the flows can get the properties without making any changes.

感谢您的所有帮助人员.让我做了很多思考.

Thanks for all your help guys. Made me do a lot of thinking.

关于, Zulfiqar

Regards, Zulfiqar

推荐答案

我找到了解决方案.显然我在尝试自动连接databasePropertiesProvider类中的@Service MyService.自动装配失败,出现null信息,这是因为我对databasePropertiesProvider类进行了更多修改,以便在应用程序初始化后运行.

I found a solution. Apparently I was trying to autowire the @Service MyService in the databasePropertiesProvider class. The autowiring was failing with null due to which I made some more modifications to the databasePropertiesProvider class so that It runs after the app is initialized.

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

Now when I look at it, I realized that I dont need to connect to the database through all the service and repository layers. I moved the query execution code from the repository class to the databasePropertiesProvider class and now the properties are loaded during initialization time and the flows can get the properties without making any changes.

整个代码如下所示 XML配置:-

The whole code looks like this XML Config:-

<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加载一样加载属性.

Now, I am able to load the properties the same way I used to load from mule-app.properties in the FLOWs.

这篇关于从数据库读取属性并使用它代替mule-app.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆