简体   繁体   English

设计Spring Application的最佳实践是我们需要多次创建相同上下文的方式吗?

[英]Is it best practice to design Spring Application in such a way that we need to create same context multiple times?

I am going to start a new project using Spring framework. 我将使用Spring框架开始一个新项目。 As I dont have much experience in Spring I need your help to sort out few confusions. 由于我在春季没有太多经验,因此需要您的帮助以解决一些困惑。

Lets look at use case 让我们看一下用例

My application uses Spring integration framework. 我的应用程序使用Spring集成框架。 The core functionality of my app is, 我的应用程序的核心功能是

  1. I need to poll multiple directories from file system, 我需要从文件系统中轮询多个目录,
  2. read the files(csv mostly), 读取文件(主要是csv),
  3. process some operations on them and insert them to database. 处理对它们的一些操作,然后将它们插入数据库。

Currently I have set up spring integration flow for it. 目前,我已经为其设置了Spring集成流程。 Which has inbound-chaneell-adapter for polling and then file traverse through the channels and at the end inserted into database. 它具有用于轮询的inbound-chaneell-adapter ,然后通过通道遍历文件,最后将其插入数据库。

My concerns are 我的担心是

  1. Number of directories application supposed to poll will be decided at runtime. 应该轮询的目录应用程序数将在运行时确定。 Hence I need to create inbound-chanell-adapter at runtime (as one chanell adapter can poll only one directory at once) and cant define them statically in my spring context xml (As I dont know the how many I need). 因此,我需要在运行时创建inbound-chanell-adapter (因为一个chanell适配器一次只能轮询一个目录),并且无法在我的spring context xml中静态定义它们(因为我不知道我需要多少个)。

  2. Each directory has certain properties which should be applied to the file while processing.(While going through the integration flow) 每个目录都有某些属性,这些属性应在处理时应用于文件。(在进行集成流程时)

  3. So right now what I am doing is I am loading new ClassPathXmlApplicationContext("/applicationContext.xml"); 所以,现在我正在加载new ClassPathXmlApplicationContext("/applicationContext.xml"); for each directory. 对于每个目录。 And cache the required properties in that newly created context. 并在该新创建的上下文中缓存所需的属性。 And use them at the time of processing (in <int:service-activator> ). 并在处理时使用它们(在<int:service-activator> )。

Drawbacks of current design 当前设计的缺点

  1. Separate context is created for each directory. 为每个目录创建单独的上下文。
  2. Unnecessary beans are duplicated. 多余的bean被复制。 (Database session factories and like) (数据库会话工厂之类)

So is there any way to design the application in such a way that context will not be duplicated. 因此,有什么方法可以以不会重复上下文的方式设计应用程序。 And still I can use properties of each directory throughout the integration flow at the same time??? 而且我仍然可以在整个集成流程中同时使用每个目录的属性???

Thanks in advance. 提前致谢。

See the dynamic ftp sample and the links in its readme about creating child contexts when needed, containing new inbound components. 请参阅动态ftp示例及其自述文件中的有关在需要时创建子上下文的链接,其中包含新的入站组件。

Also see my answer to a similar question for multiple IMAP mail adapters using Java configuration and then a follow-up question . 另请参阅我对使用Java配置的多个IMAP邮件适配器类似问题的回答,以及后续问题

You can also use a message source advice to reconfigure the FileReadingMessageSource on each poll to look at different directories. 您还可以使用消息源建议在每次轮询中重新配置FileReadingMessageSource以查看不同的目录。 See Smart polling . 请参阅智能轮询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM