Greta
Greta

Reputation: 328

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:

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.

Upvotes: 1

Views: 1125

Answers (2)

prost&#253; člověk
prost&#253; člověk

Reputation: 939

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

Upvotes: 0

ProgrammingEnthusiast
ProgrammingEnthusiast

Reputation: 480

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.

Upvotes: 1

Related Questions