我尝试将spring integration xml config转换为java config。我正面临一些我在下面讨论过的问题

时间:2017-06-29 06:46:37

标签: spring-integration

尝试了下面给出的两种方法,但没有奏效。我已经用下面的方法解决了我面临的问题 Xml配置:第一个配置错误:

  

错误:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.integration.mail.Pop3MailReceiver]:工厂方法' pop3MailReceiver'抛出异常;嵌套异常是java.lang.NoClassDefFoundError:javax / mail / Service

<int:channel id="receiveEmailChannel" />
    <util:properties id="javaMailProperties">
        <prop key="mail.pop3.socketFactory.fallback">false</prop>
        <prop key="mail.debug">true</prop>
        <prop key="mail.pop3.port">995</prop>
        <prop key="mail.pop3.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.pop3.socketFactory.port">995</prop>
    </util:properties>

    <int-mail:inbound-channel-adapter id="pop3ShouldDeleteTrue"
        store-uri="pop3://[user name]:[pwd]pop.gmail.com/INBOX"
        channel="receiveEmailChannel" should-delete-messages="false"
        auto-startup="true" java-mail-properties="javaMailProperties">

        <int:poller fixed-rate="5000" max-messages-per-poll="5"></int:poller>
    </int-mail:inbound-channel-adapter>




    <int:channel id="attachment-insert-channel" />
    <int-jdbc:outbound-channel-adapter
        channel="attachment-insert-channel" data-source="dataSource" query="${attachment-insert-query}">
    </int-jdbc:outbound-channel-adapter>


    <int:channel id="printSqlResult" />
    <int-jdbc:outbound-channel-adapter
        channel="printSqlResult" data-source="dataSource" query="${insert-query}">
    </int-jdbc:outbound-channel-adapter>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ap_mail" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>


</beans>

第一个java配置:

@Configuration
@EnableIntegration
public class MailConfig {

    @Bean
    public DirectChannel inputChannel() {
        return new DirectChannel();
    }

    private Properties javaMailProperties() {
        Properties javaMailProperties = new Properties();

        javaMailProperties.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        javaMailProperties.setProperty("mail.imap.socketFactory.fallback","false");
        javaMailProperties.setProperty("mail.store.protocol","imaps");
        javaMailProperties.setProperty("mail.debug","true");

        return javaMailProperties;
    }

    @Bean
    public Pop3MailReceiver pop3MailReceiver() {
        Pop3MailReceiver receiver = new Pop3MailReceiver("pop3://[un]:[pwd]@pop.gmail.com/INBOX");
        receiver.setJavaMailProperties(javaMailProperties());
        receiver.setShouldDeleteMessages(false);
        //receiver.setChannelResolver((DestinationResolver<MessageChannel>) inputChannel());
        return receiver;
    }

}

第二个java配置:删除了第一个@ Bean公共Pop3MailReceiver pop3MailReceiver()并使用了这个

    @Bean
    public IntegrationFlow pop3MailFlow() {
        return IntegrationFlows
                .from(Mail.pop3InboundAdapter("localhost", "995", "user", "pw")
                .javaMailProperties(p -> p.put("mail.debug", "true")),e -> e.autoStartup(true)
                .poller(Pollers.fixedDelay(60000)))
                .channel(MessageChannels.queue("pop3Channel"))
                .get();
    }

这个问题无法导入IntegrationFlows,Mail,即使它们在我的maven依赖项中

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.muraai.mail</groupId>
    <artifactId>mail</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-java-dsl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mail</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1 个答案:

答案 0 :(得分:0)

正如我从堆栈跟踪中的问题中看到的那样,没有类定义错误

抛出异常;嵌套异常是java.lang.NoClassDefFoundError:javax / mail / Service

这通常意味着从类路径或应用程序加载的位置缺少mail.jar,请检查类路径以查看是否存在所有jar文件。