【问题标题】:How to covert xml based spring-integration-email configuration to java based config?如何将基于 xml 的 spring-integration-email 配置转换为基于 java 的配置?
【发布时间】:2016-03-23 04:33:29
【问题描述】:

我在 Spring Boot 应用程序中使用 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>

【问题讨论】:

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


【解决方案1】:

您发布的代码(以及一些使其更易于配置的小改进)将转换为以下 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

【讨论】:

【解决方案2】:
@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)

【讨论】:

    猜你喜欢
    • 2014-09-25
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    相关资源
    最近更新 更多