2016-11-20 1 views
0

Spring Boot 2.0.0.BUILD-SNAPSHOT을 사용하여 SpringBoot 응용 프로그램 시작 실패. JavaMailSender 또는 JavaMailSenderImpl을 autowiring 할 때 문제가 있습니다. JavaMailSender에 대해 @Autowired을 구성하면 오류가 발생합니다.JavaMailSender - 버전 2.0.0 - 스냅 샷

*************************** 
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입니다.


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);*/ 

    } 
} 

<?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.mail 항아리/스프링 상황에 맞는 지원을 추가하여 시도했지만이 일을 일부러.

application.properties: 
spring.mail.host=smtp.gmail.com 
[email protected] 
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); 
    } 
} 

안내하십시오.

업데이트 나에서 Nov22는 :

내가 새로운 클래스를 만들고 해당 클래스의 자동 구성을 사용하는 경우, JavaMailSender 및 JavaMailSenderImpl가 제대로 autowire가되어 봄 부팅 응용 프로그램이 성공적으로 시작됩니다. 이미 application.class에서도 enableautoconfiguration을 사용하고 있습니다. enableautoconfiguration으로 구성된 두 클래스를 가지고 있어도 괜찮습니까? JavaMailSender를 autowire하는 다른 방법이 있습니까?

@EnableAutoConfiguration 
public class MailConfig { 

    @Autowired 
    private JavaMailSenderImpl mailSender; 
} 

답변

0

변경

@Autowired 
private JavaMailSenderImpl mailSender; 

@Autowired 
private JavaMailSender mailSender; 

그 이유는 빈 클래스가 인터페이스를 구현하는 경우 기본적으로 봄, JDK 프록시를 생성하는,이다

합니다. JDK 프록시는 Bean 클래스와 동일한 인터페이스를 구현하지만 클래스를 확장하지는 않습니다.

+0

감사합니다. JavaMailSender에서 이미 autowired를 시도했지만 실패한 동일한 시작을 제공하고있었습니다. 구성으로 annotate와 별도의 클래스를 만들고 주석이 달린 Bean 태그가있는 JavaMailSender를 만들어야합니까? 초보자 용 메일을 시작하지 말고, JavaMailSender를 생성하고 스스로 주입하십시오. – user2057006

+0

기본 애플리케이션 클래스의 코드를 표시 할 수 있습니까? – dunni

+0

가 주 클래스 코드에 추가되었습니다. – user2057006

1

글쎄, 너는 Mailer을 전혀 구성하지 않은 것처럼 보입니다. 따라서 Spring은 그것을 연결하기 위해 빈을 찾을 수 없습니다. 종속성 문제인 경우 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; 
    } 
} 

원래 answer here

+0

@ AntJavaDev - 답변 해 주셔서 감사합니다. javaMailService 메소드의 Annotated Bean을 통해 구성 클래스를 작성하고 JavaMailSender 인스턴스를 작성해야합니까? 추가 구성 클래스를 작성하지 않고도 스프링 부트 스타터 메일을 만들지 않고 자체적으로 autowire하지 않습니까? – user2057006

+0

최신 문서를 잘 읽고 내가 게시 한 원래 답변을 확인하면 Spring Boot는 application.properties에 지정된 매개 변수를 추가하기 만하면 자동으로 구성해야합니다. 그러나 많은 문제가있는 것을 볼 수 있듯이, 가장 간단한 해결책은 직접 정의하는 것입니다. – AntJavaDev

+0

또한이 [post] (http : // stackoverflow.com/questions/22483407/send-email-with-spring-by-java-annotations) 또한 귀하의 문제와 관련이 있기 때문에 어쨌든 문제가 있는지 확인하기 위해 구성으로 정의하려 했습니까? 그렇지 않으면 이것이 작동하지 않는다면, 'javax.mail' jar – AntJavaDev