我是Spring的新手,我不喜欢代码重复。我写了一个工作正常的ImapAdapter:@Componentpublic class GeneralImapAdapter { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private EmailReceiverService emailReceiverService; @Bean @InboundChannelAdapter(value = "emailChannel", poller = @Poller(fixedDelay = "10000", taskExecutor = "asyncTaskExecutor")) public MessageSource<javax.mail.Message> mailMessageSource(MailReceiver imapMailReceiver) { return new MailReceivingMessageSource(imapMailReceiver); } @Bean @Value("imaps://<login>:<pass>@<url>:993/inbox") public MailReceiver imapMailReceiver(String imapUrl) { ImapMailReceiver imapMailReceiver = new ImapMailReceiver(imapUrl); imapMailReceiver.setShouldMarkMessagesAsRead(true); imapMailReceiver.setShouldDeleteMessages(false); // other setters here return imapMailReceiver; } @ServiceActivator(inputChannel = "emailChannel", poller = @Poller(fixedDelay = "10000", taskExecutor = "asyncTaskExecutor")) public void emailMessageSource(javax.mail.Message message) { emailReceiverService.receive(message); }}但是我想要大约20个这样的适配器,唯一的区别是imapUrl。没有代码重复怎么办?
2 回答
米脂
TA贡献1836条经验 获得超3个赞
我并不是在建议您需要动态渠道;我建议您可以使用该示例使用的相同技术,以创建由属性驱动的具有相同配置的多个“ cookie切割器”实例。即AnnotationConfigApplicationContext
从您的配置类创建一个新的;在其环境中加载实例的属性,并通过刷新来创建上下文。从而避免代码重复。
- 2 回答
- 0 关注
- 678 浏览
添加回答
举报
0/150
提交
取消