SpringBoot应用程序启动失败,原因是autowire JavaMailSender-版本2.0.0-snapshot
我使用的是SpringBoot应用程序启动失败,原因是autowire JavaMailSender-版本2.0.0-snapshot,spring,spring-mvc,spring-boot,spring-boot-maven-plugin,Spring,Spring Mvc,Spring Boot,Spring Boot Maven Plugin,我使用的是springboot2.0.0.BUILD-SNAPSHOT。 自动连接JavaMailSender或JavaMailSenderImpl时出现问题。 如果我为JavaMailSender配置@Autowired,我会得到以下错误 *************************** APPLICATION FAILED TO START *************************** Description: Field mailSender in com.hm.ass
springboot2.0.0.BUILD-SNAPSHOT
。
自动连接JavaMailSender
或JavaMailSenderImpl
时出现问题。
如果我为JavaMailSender
配置@Autowired
,我会得到以下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mailSender in com.hm.assetmanagment.service.MailService required a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' that could not be found.
- Bean method 'mailSender' not loaded because @ConditionalOnClass did not find required class 'javax.mail.internet.MimeMessage'
Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' in your configuration.
下面是我的pom.xml,其中包含spring boot starter电子邮件
<?xml version="1.0" encoding="UTF-8"?>
<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.hm.assetmanagement</groupId>
<artifactId>AssetManagementSystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AssetManagementSystem</name>
<description>AssetManagementSystem</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<!-- <exclusions>
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
</exclusions>-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
我尝试在依赖项中手动添加javax.mailjar/spring上下文支持,但没有成功
application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.username=xxx@gmail.com
spring.mail.password=xxx
spring.mail.port=587
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.thymeleaf.cache=false
debug=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Main Class:
package com.hm.assetmanagment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories
@SpringBootApplication
public class AssetManagementSystemApplication {
public static void main(String[] args) {
SpringApplication.run(AssetManagementSystemApplication.class, "--debug");
//SpringApplication.run(AssetManagementSystemApplication.class, args);
}
}
请导游
我于2002年11月22日更新:
若我创建了新类并在该类上启用了自动配置,JavaMailSender和JavaMailSenderImpl将正确地自动连接,spring启动应用程序将成功启动。我已经在application.class中启用了自动配置。使用enableautoconfiguration配置两个类可以吗?有没有其他方法可以自动连接JavaMailSender
@EnableAutoConfiguration
public class MailConfig {
@Autowired
private JavaMailSenderImpl mailSender;
}
改变
到
原因是,默认情况下,如果bean类实现接口,Spring会创建JDK代理。JDK代理实现了与bean类相同的接口,但没有扩展该类。看来您根本没有配置
邮件程序,因此Spring无法找到bean来连接它。如果这是一个依赖性问题,那么您将得到一个ClassNotFoundException
或NoClassDefException
,这不是您的情况。
无论如何,请尝试如下配置您的邮件程序:
@Configuration
public class MailConfig {
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("myHost");
javaMailSender.setPort(25);
javaMailSender.setJavaMailProperties(getMailProperties());
return javaMailSender;
}
private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.debug", "false");
return properties;
}
}
原始通过查看代码,您需要添加以下属性才能工作
示例app.yml文件
春天
邮寄:
默认编码:UTF-8
主机:本地主机
港口:8025
协议:smtp
测试连接:false
properties.mail.smtp:
starttls.enable:true
Here is the reference
https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/boot-features-email.html
确保您正在阅读应用程序属性谢谢您的回复。我已经在JavaMailSender上尝试了自动连线,但它给出了相同的启动失败问题。我是否需要使用带注释的配置创建单独的类,并使用带注释的Bean标记创建JavaMailSender?不要用spring启动starter mail,创建JavaMailSender并自行注入它?您能显示主应用程序类的代码吗?添加了MainClass代码。@AntJavaDev感谢您的回复。是否必须从javaMailService方法通过带注释的Bean创建配置类和JavaMailSender实例?spring boot starter mail是否在不创建其他配置类的情况下自行创建和autowire?根据最新文档,如果您检查了我发布的原始答案,spring boot应该通过将指定参数添加到application.properties来自动配置它。但是,正如我所看到的,设置它有很多困难,最简单的解决方案是自己定义它。还要检查这个,因为它可能与您的问题有关。无论如何,您是否尝试将其定义为一种配置,以查看这是否是问题所在?否则,如果这也不起作用,那么您可能对javax.mail
jaryes有一个依赖项混合。我试图将其定义为配置,但它不起作用。让我检查一下依赖混合,然后很快回来。是的,如果我将其定义为配置,我会遇到相同的错误
@Autowired
private JavaMailSender mailSender;
@Configuration
public class MailConfig {
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("myHost");
javaMailSender.setPort(25);
javaMailSender.setJavaMailProperties(getMailProperties());
return javaMailSender;
}
private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.debug", "false");
return properties;
}
}
Here is the reference
https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/boot-features-email.html