如何将基于xml的spring-integration-email配置转换为基于java的配置?

时间:2016-03-23 04:33:29

标签: java spring-boot spring-integration spring-java-config

我在spring启动应用程序中使用spring-integration-email来接收电子邮件。我在网上找到的所有例子都使用基于xml的配置。

我在我的应用程序中使用基于java的配置,我想在spring-integration-email中使用相同的配置。

下面是我的基于xml的配置,它可以正常工作。如何将其转换为基于java的配置?

<util:properties id="javaMailProperties">
    <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
    <prop key="mail.imap.socketFactory.fallback">false</prop>
    <prop key="mail.store.protocol">imaps</prop>
    <prop key="mail.debug">${imap.debug}</prop>
</util:properties>

<mail:inbound-channel-adapter id="imapAdapter"
                              store-uri="${imap.uri}"
                              channel="recieveEmailChannel"
                              should-delete-messages="false"
                              should-mark-messages-as-read="true"
                              auto-startup="true"
                              java-mail-properties="javaMailProperties">
    <int:poller fixed-delay="${imap.poolerSecondsDelay}" time-unit="SECONDS"/>
</mail:inbound-channel-adapter>

<int:channel id="recieveEmailChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="logger" level="DEBUG"/>

<int:service-activator input-channel="recieveEmailChannel" ref="emailReceiverService" method="receive"/>

<bean id="emailReceiverService" class="com.sts.app.email.service.EmailService">
</bean>

2 个答案:

答案 0 :(得分:0)

您发布的代码(以及一些小的改进使其更易于配置)将转换为以下Java配置:

@Configuration 
public class MailConfig {

    @Value("${email.host}")
    private String host;

    @Value("${email.from}")
    private String from;

    @Value("${email.subject}")
    private String subject;

    @Bean
    public JavaMailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        return javaMailSender;
    }

    @Bean
    public SimpleMailMessage simpleMailMessage() {
       SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
       simpleMailMessage.setFrom(from);
       simpleMailMessage.setSubject(subject);
       return simpleMailMessage;
    }
}

还快速查看此link

答案 1 :(得分:0)

@Bean
@InboundChannelAdapter(value = "testReceiveEmailChannel", poller = @Poller(fixedDelay = "200000", taskExecutor = "asyncTaskExecutor"))
public MessageSource<javax.mail.Message> mailMessageSource(MailReceiver mailReceiver) {
    MailReceivingMessageSource mailReceivingMessageSource = new MailReceivingMessageSource(mailReceiver);
    // other setters here
    return mailReceivingMessageSource;
}

@Bean
public MailReceiver imapMailReceiver(@Value("imaps://${login}:${pass}@${host}:993/inbox") storeUrl) {
     ImapMailReceiver imapMailReceiver = new ImapMailReceiver(storeUrl);
        // other setters here
     return imapMailReceiver;
}

Spring Integration Java DSL可能如下所示:

    @Bean
    public IntegrationFlow imapMailFlow() {
        return IntegrationFlows
                .from(s -> s.imap("imap://user:pw@localhost:" + imapPort + "/INBOX")
                                .searchTermStrategy(this::fromAndNotSeenTerm)
                                .javaMailProperties(p -> p.put("mail.debug", "false")),
                        e -> e.autoStartup(true)
                                .poller(p -> p.fixedDelay(1000)))
                .channel(MessageChannels.queue("imapChannel"))
                .get();
    }

另请参阅问题及其讨论:spring receive emails without xml (using annotations only)