开发者

Spring integration : replace xml configured bean property dynamically?

开发者 https://www.devze.com 2023-03-15 22:48 出处:网络
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 propertie

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>


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消