Spring集成:动态替换xml配置的bean属性?

发布于 2024-11-17 10:47:57 字数 725 浏览 5 评论 0原文

我正在尝试在 Spring 集成的帮助下做一个 ftp 轮询器,并且轮询器与 xml 配置配合得很好。现在我希望能够动态设置轮询器的一些属性,例如 cron 表达式或轮询率,使其可以通过 java 代码进行配置并将其链接到 Web 界面。

我已经看到了很多围绕该主题的主题,但没有任何明确的内容可以做到这一点。
有没有经典的方法可以做到这一点?
可以用 SpeL 完成吗?

我的 bean poller 在 XML 中的声明如下:

<int-ftp:inbound-channel-adapter id="ftpInbound"
    channel="ftpChannel" session-factory="ftpClientFactory"
    filename-regex=".*\.tmp$" auto-create-local-directory="true"
    delete-remote-files="false" remote-directory="/cft-polling" local-directory="file:target/ftp-output" >
    <int:poller fixed-rate="1000" />
</int-ftp:inbound-channel-adapter>

<int:channel id="ftpChannel">
    <int:queue />
</int:channel>

I'm trying to do a ftp poller with the help of Spring integration and the poller works great with the xml configuration. Now I would like to be able to dynamically set some properties of the poller like the cron-expression or the polling rate to make it configurable by java code and link it to a web interface.

I have seen a lot of topics around the subject but nothing really clear to do that.
Is there a classic way of doing that ?
Can it be done with SpeL ?

My bean poller declaration in XML is as follows :

<int-ftp:inbound-channel-adapter id="ftpInbound"
    channel="ftpChannel" session-factory="ftpClientFactory"
    filename-regex=".*\.tmp$" auto-create-local-directory="true"
    delete-remote-files="false" remote-directory="/cft-polling" local-directory="file:target/ftp-output" >
    <int:poller fixed-rate="1000" />
</int-ftp:inbound-channel-adapter>

<int:channel id="ftpChannel">
    <int:queue />
</int:channel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2024-11-24 10:47:57

我不确定这里是否有足够的可靠答案,但假设 ftp poller 是在 spring 容器中定义和管理的,并且假设有适当的附件来修改它的属性......您将能够更改它的就像设置任何其他对象一样。

首先,您必须获取 Spring 托管对象的引用,您可以通过让您的类之一实现 ApplicationContextAware 从而公开 Spring 上下文来完成此操作。

然后只需从上下文中获取 bean 并更新它的属性即可。

public class MyManagedClass implements ApplicationContextAware {
   private ApplicationContext springContext;

   public void changeBeansProperty(){
      MyFtpPoller poller = (MyFtpPoller) springContext.getBean("ftpInbound");
      poller.setCronExpress("12 12 * * * *");
   }

   public void setApplicationContext(ApplicationContext applicationContext) {
       this.springContext = applicationContext;
   }

}

I'm not sure there is enough here for a solid answer, but assuming that the ftp poller is defined and managed in the spring container, and assuming there are proper accessores to modify it's properties...that you will be able to change it's setting just like you would any other object.

First you would have to get a reference of the spring managed object, you can do this by having one of your classes implement ApplicationContextAware thereby exposing the Spring context.

Then it's just a matter of getting the bean from the context and updating it's property.

public class MyManagedClass implements ApplicationContextAware {
   private ApplicationContext springContext;

   public void changeBeansProperty(){
      MyFtpPoller poller = (MyFtpPoller) springContext.getBean("ftpInbound");
      poller.setCronExpress("12 12 * * * *");
   }

   public void setApplicationContext(ApplicationContext applicationContext) {
       this.springContext = applicationContext;
   }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文