appzone_oto
appzone_oto

Reputation: 333

How do i resolve Consider defining a bean of type 'java.lang.String' in your configuration?

I am getting the following error

Description:

Parameter 0 of constructor in com.gambeat.site.utility.email.EmailStatus required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

This is the Email Status class

    package com.gambeat.site.utility.email;


import org.springframework.stereotype.Component;

@Component
public class EmailStatus {

    public static final String SUCCESS = "SUCCESS";

    public static final String ERROR = "ERROR";

    private final String to;
    private final String subject;
    private final String body;

    private String status;
    private String errorMessage;


    public EmailStatus(String to, String subject, String body) {
        this.to = to;
        this.subject = subject;
        this.body = body;
    }

    public EmailStatus success() {
        this.status = SUCCESS;
        return this;
    }

    public EmailStatus error(String errorMessage) {
        this.status = ERROR;
        this.errorMessage = errorMessage;
        return this;
    }

    public boolean isSuccess() {
        return SUCCESS.equals(this.status);
    }

    public boolean isError() {
        return ERROR.equals(this.status);
    }

    public String getTo() {
        return to;
    }

    public String getSubject() {
        return subject;
    }

    public String getBody() {
        return body;
    }

    public String getStatus() {
        return status;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

}

this is the EmailSender and HtmlEmailSender which calls the EmailStatus class respectively.

    package com.gambeat.site.utility.email;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

@Component
public class EmailSender {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailSender.class);

    @Autowired
    private JavaMailSender javaMailSender;

    public EmailStatus sendPlainText(String to, String subject, String text) {

        return sendM(to, subject, text, false);

    }

    public EmailStatus sendHtml(String to, String subject, String htmlBody) {

        return sendM(to, subject, htmlBody, true);

    }

    private EmailStatus sendM(String to, String subject, String text, Boolean isHtml) {

        try {

            MimeMessage mail = javaMailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(mail, true);

            helper.setTo(to);

            helper.setSubject(subject);

            helper.setText(text, isHtml);

            javaMailSender.send(mail);

            LOGGER.info("Send email '{}' to: {}", subject, to);

            return new EmailStatus(to, subject, text).success();

        } catch (Exception e) {

            LOGGER.error(String.format("Problem with sending email to: {}, error message: {}", to, e.getMessage()));

            return new EmailStatus(to, subject, text).error(e.getMessage());
        }
    }
}

HtmlEmailSender class

    package com.gambeat.site.utility.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Component
public class EmailHtmlSender {

    @Autowired
    private EmailSender emailSender;

    @Autowired
    private TemplateEngine templateEngine;

    public EmailStatus send(String to, String subject, String templateName, Context context) {

        String body = templateEngine.process(templateName, context);

        return emailSender.sendHtml(to, subject, body);
    }
}

The problem started when I called the HtmlSendEmail in my service class

    package com.gambeat.site.services.implementation;

import com.gambeat.site.entities.User;
import com.gambeat.site.services.NotificationService;
import com.gambeat.site.utility.email.EmailHtmlSender;
import com.gambeat.site.utility.email.EmailStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;

/**
 * Created by Oto-obong on 30/10/2017.
 */

@Service
public class DefaultNotificationService implements NotificationService {

    private JavaMailSender javaMailSender;

    private EmailHtmlSender emailHtmlSender;

    @Autowired
    public DefaultNotificationService(JavaMailSender javaMailSender, EmailHtmlSender emailHtmlSender){

        this.javaMailSender = javaMailSender;

        this.emailHtmlSender = emailHtmlSender;

    }

    @Override
    public EmailStatus SignUpConfirmation(User user, String url) {

        Context context = new Context();

        context.setVariable("salutation", String.format("Hello s%", user.getUserName()));

        context.setVariable("title", "Welcome to Gambeat");

        context.setVariable("message", "Lorem Lorem Lorem");

        context.setVariable("link", url);

        EmailStatus emailStatus = emailHtmlSender.send(user.getEmail(), "Welcome to Gambeat", "email/welcome", context);

        return emailStatus;

    }

    @Override
    public EmailStatus BroadCastViaEmail(String message) {

        return null;

    }

    @Override
    public EmailStatus BroadCastViaEmail(User user, String message) {

        return null;

    }

    @Override
    public EmailStatus SendTokenViaEmail(User user, String token) {

        Context context = new Context();

        context.setVariable("salutation", String.format("Hello s%", user.getUserName()));

        context.setVariable("title", "Welcome to Gambeat");

        context.setVariable("description", "Lorem Lorem Lorem");

        EmailStatus emailStatus = emailHtmlSender.send(user.getEmail(), "Welcome to Gambeat", "email/welcome", context);

        return emailStatus;

    }
}

Based on peoples recommendation i was told to add the Email Status package class to my component scan, but it was to no avail, can someone please help me out?

Upvotes: 0

Views: 5044

Answers (2)

Priyamal
Priyamal

Reputation: 2969

EmailStatus class is registered as a spring bean with using @Component annotation.in that case you need to have a default constructor inside the class.

@Component
public class EmailStatus {
}

after being registered as a spring bean that can be autowired

@Autowired
EmailStatus emailStatus;

but after looking at your code you never autowire a EmailStatus bean . so the the simplest thing that you can do is to remove the @Component annotation on the EmailStatus class. and this will work fine.

Upvotes: 2

shahaf
shahaf

Reputation: 4973

the problem is in your EmailStatus class, because you defined a constructor that override the default, spring don't know how to build this class, it don't know how to inject the strings args to the contractor public EmailStatus(String to, String subject, String body) {...

remove this contractor and define a default one if you need to init some variables (otherwise, the compiler will define it automatically)

use setters to set the to, subject and body

Upvotes: -1

Related Questions