How to prevent duplicate Spring Integration service activations when polling directory

后端 未结 1 1888
忘掉有多难
忘掉有多难 2021-01-23 21:45

I have a Spring Integration directory poller:




        
1条回答
  •  粉色の甜心
    2021-01-23 22:19

    This answer is based on the tip from Gary Russell in the comments above.

    The reason for double-processing of files is that the root and web configs were both initialising file system listeners, and therefore processing each file twice.

    My approach to avoiding having the file listeners in multiple contexts was as follows.

    First define a web config which only picks up classes under the "web" package.

    @Configuration
    @ComponentScan(basePackages = { "com.myapp.web" })
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Override
        public void configureDefaultServletHandling(
                DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    }
    

    Create separate root configs which only load beans which are not in the "web" package. i.e.

    @Configuration
    @ComponentScan(basePackages = { "com.myapp.services" })
    public class ServicesConfig {
    }
    

    An additional factor in the configuration that took a little while to work out, was that my servlet filters and web security config needed to be in the 'root' context rather than the web context.

    0 讨论(0)
提交回复
热议问题