gpt4 book ai didi

由于 Autowiring JavaMailSender,SpringBoot 应用程序启动失败 - 版本 2.0.0-snapshot

转载 作者:行者123 更新时间:2023-12-01 10:32:42 34 4
gpt4 key购买 nike

我正在使用 Spring Boot 2.0.0.BUILD-SNAPSHOT .
我在 Autowiring 时遇到问题 JavaMailSenderJavaMailSenderImpl .
如果我配置 @AutowiredJavaMailSender ,我得到以下错误。

***************************
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>
package com.hm.assetmanagment.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailService {

@Autowired
private JavaMailSenderImpl mailSender;

@RequestMapping("/assets/allocateemail/{name}/{email}/{assetid}")
private void sendAssetAllocationEmail(String name, String email, String assetid) {
/*SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(email);
simpleMailMessage.setSubject("Asset Allocation Confirmation");
simpleMailMessage.setText("Hello " + name + "," + assetid + "\n" + " - Asset allocated to you");
mailSender.send(simpleMailMessage);*/

}
}

我尝试通过依赖手动添加 javax.mail jar/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);
}
}

请指导。

来自我 11 月 22 日的更新:

如果我创建新类并在该类上启用自动配置,则 JavaMailSender 和 JavaMailSenderImpl 会正确 Autowiring ,并且 Spring Boot 应用程序会成功启动。我已经在 application.class 中启用了自动配置。使用 enableautoconfiguration 配置两个类可以吗?有没有其他方法可以 Autowiring JavaMailSender?
@EnableAutoConfiguration
public class MailConfig {

@Autowired
private JavaMailSenderImpl mailSender;
}

最佳答案

好吧,您似乎还没有配置 Mailer ,因此 Spring 无法找到 bean 来连接它。如果这是一个依赖问题,那么你会得到一个 ClassNotFoundExceptionNoClassDefException ,这不是你的情况。
无论如何尝试像这样配置您的邮件程序:

@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;
}
}

原装 answer here

关于由于 Autowiring JavaMailSender,SpringBoot 应用程序启动失败 - 版本 2.0.0-snapshot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708272/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com