在Camel中初始化bean的最佳方式(人类可读和最快的运行时(比方说,已编译))?
我需要一个bean将它发送到JPA端点。输入是使用CSV创建的地图。看起来像一个微不足道的任务,但我刚刚开始使用Camel,请帮助。
目前我已经设法将Map转换为更复杂的Map,它与我的bean具有相同的结构。我可以编写自己的代码来遍历它,使用Apache Beanutils实例化列表和列表成员,但我认为应该有一种更快的方法来从原始Map初始化bean,而不创建中间Map。
以下是我目前的情况:
<route id="AgencyCsvInput" errorHandlerRef="agencyDeadLetters">
<from uri="direct:agencyCsvInput"/>
<split streaming="true">
<method ref="csvSplitter" method="tokenizeReader"/> <!-- prepends the first line of file for every subsequent line -->
<log message="splitted: ${body}" loggingLevel="DEBUG"/>
<setHeader headerName="inputRegistryLines">
<simple>${body}</simple>
</setHeader>
<unmarshal>
<csv delimiter=";" useMaps="true" />
</unmarshal>
<log message="unmarshalled: size: ${body.size()}, ${body}" loggingLevel="DEBUG"/>
<filter>
<simple>${body.size()} == 1</simple><!-- be sure to have spaces around an operator -->
<log message="filtered: listItem: ${body[0]['PATRONYMIC']}, list: ${body}" loggingLevel="DEBUG"/>
<setProperty propertyName="parser">
<simple>ref:patternParser</simple>
</setProperty>
<transform>
<spel><![CDATA[#{
{
dateOpen: properties['parser'].date(body[0]['ACTIVATION_DATE'], 'dd.MM.yyyy H:mm'),
balances: {
balance: {
{
typeCd: 'loanAmount',
amount: properties['parser'].comma(body[0]['GRANTED_CREDIT']),
currencyCd: body[0]['CURRENCY']
},
{
typeCd: 'fee',
amount: properties['parser'].comma(body[0]['Коммиссии']),
currencyCd: body[0]['CURRENCY'],
comment: 'Коммиссии'
},
{
typeCd: 'fine',
amount: properties['parser'].comma(body[0]['Пени']),
currencyCd: body[0]['CURRENCY'],
comment: 'Пени'
},
{
typeCd: 'penalty',
amount: properties['parser'].comma(body[0]['Штрафы']),
currencyCd: body[0]['CURRENCY'],
comment: 'Штрафы'
}
}
},
persons: {
{
typeCd: 'mainParty',
person: {
{
typeCd: 'individual',
names: {
name: {
{
typeCd: 'currentName',
lastName: body[0]['LAST_NAME'],
firstName: body[0]['FIRST_NAME'],
middleName: body[0]['PATRONYMIC']
}
}
},
birthDate: properties['parser'].date(body[0]['BIRTH_DATE'], 'dd.MM.yyyy H:mm'),
contacts: {
contact: {
{
typeCd: 'residentialAddress',
technologyCd: 'physical',
relationCd: 'home',
address: body[0]['COMMENTS']
},
{
typeCd: 'officeAddress',
technologyCd: 'physical',
relationCd: 'work',
address: body[0]['COMMENTS_WORK']
}
}
}
}
}
}
}
}
}]]></spel>
</transform>
<log message="transformed: ${body}" loggingLevel="DEBUG"/>
<to uri="direct:objectToDb"/>
</filter>
</split>
</route>
通过几个自定义日期和数字解析器函数以及SpEL内联Map / List实例化语法,查看以简单易懂的方式转换Map是多么容易。当我使用Hibernate&#34;动态地图&#34;时,它就像一个魅力,所以我的持久对象实际上是java.util.Maps。我希望有一种简单的方法来实例化bean。它有一些List类型的字段,列出的对象的类型是已知的,它们反过来可能有其他列表作为它们的字段 - 所有这些都基于带有getter和setter的bean。
可能,BeanShell有一个内联bean初始化语法?或者Camel有办法调用getter / setter并阅读Map?
我想避免像这样的脚本:
result = new myBean();
result.dateOpen = properties['parser'].date(body[0]['ACTIVATION_DATE'], 'dd.MM.yyyy H:mm');
balances = new Balances();
result.balances = balances;
balance = new List<Balance>();
reallyBalance = new Balance();
balance.add(balance);
reallyBalance.typeCd="loanAmount";
....
我觉得它很难理解。
这是一个纯粹的Camel问题,但我也将它标记为Beanshell,希望提示内联bean init语法,如:
bean = new MyBean(){dateOpen= properties['parser'].date(body[0]['ACTIVATION_DATE'], 'dd.MM.yyyy H:mm')....};