【问题标题】:Spring integration : replace xml configured bean property dynamically?Spring集成:动态替换xml配置的bean属性?
【发布时间】:2011-09-24 22:36:30
【问题描述】:

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

我已经看到了很多关于这个主题的主题,但没有什么真正明确的。
有没有经典的方法?
可以用 SpeL 完成吗?

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

<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>

【问题讨论】:

  • 非常感谢您对此投票的一些解释。
  • 我这是某人的错误

标签: spring spring-integration


【解决方案1】:

我不确定这里是否有足够的答案,但假设 ftp 轮询器是在 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;
   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-30
    • 2015-11-08
    • 1970-01-01
    • 2013-11-16
    • 2016-12-22
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多