简体   繁体   中英

Why can I start my application using mvn spring-boot:run while IntelliJ run doen´t work?

When I use the command mvn spring-boot:run the project compiles and starts perfectly. However, when I use the play-button in my IDE (IntelliJ), I get the following error:

Description:
Parameter 3 of constructor in com.example.module.services.PdfService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found. 

What might be the reason for this? I`d love to use the debugging and dev tools provided by the IDE.

What I 've tried:

  • Select Build->Rebuild Project
  • Clicking File>Invalidate caches/ restart
  • mvn clean -> Build -> Make Project
  • Maven -> Reimport
  • Checked that there is no excludes in Preferences | Build, Execution, Deployment | Compiler | Excludes

My pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</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-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

The Service in which the error occurs

@Service
public class PdfService {

private final Logger logger = LoggerFactory.getLogger(PdfService.class);

private PdfCreator pdfCreator;

private ConfigProperties properties;

private ReceiptService receiptService;

private JavaMailSender javaMailSender;

private MessageSourceAccessor messageSourceAccessor;

@Autowired
public PdfService(PdfCreator pdfCreator,
                  ConfigProperties properties,
                  ReceiptService receiptService,
                  JavaMailSender javaMailSender,
                  MessageSourceAccessor messageSourceAccessor) {
    this.pdfCreator = pdfCreator;
    this.properties = properties;
    this.receiptService = receiptService;
    this.javaMailSender = javaMailSender;
    this.messageSourceAccessor = messageSourceAccessor;
}

Help is very much appreciated.

You need to add org.springframework.mail.javamail.JavaMailSender.jar as dependency as it is missing. You may follow the following steps to fix in intellij.

  1. Download the jar file.
  2. In IntelliJ Idea IDE, Go to File > Project Structure.
  3. Select Modules.
  4. In the dependecies section, select (+) icon > Select JARs or Directories
  5. Paste link to the location of the JAR file
  6. Select OK and Apply

Now, it will be fixed.

You need to create Configuration class and tell them to container with @Bean I just tried. It works for me.

@Configuration
public class AppConfiguration {

    @Bean
    public JavaMailSender javaMailSender(){
        return new JavaMailSenderImpl();
    }

}

Please check my Gist on the below url,

https://gist.github.com/thangavel-projects/9c30e6fe141755cf471c9d574e7341b2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM