springboot javamailsender stackoverflow

时间:2017-02-25 08:40:25

标签: java spring-boot

我尝试使用spring-boot-starter-mail配置邮件发件人,但我收到了stackoverflow异常:

  

无法实例化[org.springframework.mail.javamail.JavaMailSender]:工厂方法' mailSender'抛出异常;嵌套异常是java.lang.StackOverflowError

这是我的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>

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

<groupId>com.test</groupId>
<artifactId>spring-boot-web</artifactId>
<version>0.0.1</version>

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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
</dependencies>

<packaging>war</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

这是我的MailConfig类

package com.test.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class MailConfig {



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

    @Value("${mail.smtp.port}")
    private Integer port;

    @Value("${mail.smtp.user}")
    private String user;

    @Value("${mail.smtp.pass}")
    private String password;

    @Bean
    public JavaMailSender mailSender(){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(user);
        mailSender.setPassword(password);

        return mailSender();
    }

}

这是一个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailSender' defined in class path resource [es/xavi/caveofprogramming/configuration/MailConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.mail.javamail.JavaMailSender]: Factory method 'mailSender' threw exception; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
Caused by: java.lang.StackOverflowError: null
    at java.io.UnixFileSystem.getBooleanAttributes0(Native Method) ~[na:1.8.0_91]
    at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:242) ~[na:1.8.0_91]
    at java.io.File.exists(File.java:819) ~[na:1.8.0_91]
    at sun.misc.URLClassPath$FileLoader.getResource(URLClassPath.java:1245) ~[na:1.8.0_91]
    at sun.misc.URLClassPath$FileLoader.findResource(URLClassPath.java:1212) ~[na:1.8.0_91]
    at sun.misc.URLClassPath$1.next(URLClassPath.java:240) ~[na:1.8.0_91]
    at sun.misc.URLClassPath$1.hasMoreElements(URLClassPath.java:250) ~[na:1.8.0_91]
    at java.net.URLClassLoader$3$1.run(URLClassLoader.java:601) ~[na:1.8.0_91]
    at java.net.URLClassLoader$3$1.run(URLClassLoader.java:599) ~[na:1.8.0_91]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_91]
    at java.net.URLClassLoader$3.next(URLClassLoader.java:598) ~[na:1.8.0_91]
    at java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623) ~[na:1.8.0_91]
    at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45) ~[na:1.8.0_91]
    at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54) ~[na:1.8.0_91]
    at javax.activation.SecuritySupport$3.run(SecuritySupport.java:79) ~[na:1.8.0_91]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_91]
    at javax.activation.SecuritySupport.getResources(SecuritySupport.java:73) ~[na:1.8.0_91]
    at javax.activation.MimetypesFileTypeMap.loadAllResources(MimetypesFileTypeMap.java:168) ~[na:1.8.0_91]
    at javax.activation.MimetypesFileTypeMap.<init>(MimetypesFileTypeMap.java:110) ~[na:1.8.0_91]
    at javax.activation.MimetypesFileTypeMap.<init>(MimetypesFileTypeMap.java:256) ~[na:1.8.0_91]
    at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.createFileTypeMap(ConfigurableMimeFileTypeMap.java:147) ~[spring-context-support-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.getFileTypeMap(ConfigurableMimeFileTypeMap.java:119) ~[spring-context-support-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.afterPropertiesSet(ConfigurableMimeFileTypeMap.java:106) ~[spring-context-support-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.<init>(JavaMailSenderImpl.java:105) ~[spring-context-support-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at com.test.configuration.MailConfig.mailSender(MailConfig.java:28) ~[classes/:na]
    at com.test.configuration.MailConfig$$EnhancerBySpringCGLIB$$6d395315.CGLIB$mailSender$0(<generated>) ~[classes/:na]
    at com.test.configuration.MailConfig$$EnhancerBySpringCGLIB$$6d395315$$FastClassBySpringCGLIB$$865330b3.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]

这部分重复了很多次:

at com.test.configuration.MailConfig.mailSender(MailConfig.java:28) ~[classes/:na]
    at com.test.configuration.MailConfig$$EnhancerBySpringCGLIB$$6d395315.CGLIB$mailSender$0(<generated>) ~[classes/:na]
    at com.test.configuration.MailConfig$$EnhancerBySpringCGLIB$$6d395315$$FastClassBySpringCGLIB$$865330b3.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]

任何想法?

感谢

1 个答案:

答案 0 :(得分:1)

return mailSender();应为return mailSender;。使用括号,您只需再次递归调用该方法,而不会出现任何中断条件。但是你想要返回该字段,所以删除括号。