通过字段表示的不满意依赖:没有类型的限定bean

时间:2016-10-20 20:57:21

标签: spring spring-mvc

接口的实现是使用抽象类而不是具体类来实现的。

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ contactController ”的bean时出错:

通过字段“ emailService ”表示的不满意的依赖关系: 找不到依赖项[com.proto.backendservice.EmailService]类型的限定bean [com.proto.backendservice.EmailService ]:预计至少有1个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} ;

嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为[com.proto.backendservice.EmailService]找到[com.proto.backendservice.EmailService]类型的限定bean:期望至少有1个符合条件的bean作为此依赖关系的autowire候选者。

依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}。

的ContactController

package com.proto.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.proto.backendservice.EmailService;
import com.proto.domainFrontend.FeedBackPojo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class ContactController {

    private static final Logger LOG=LoggerFactory.getLogger(ContactController.class);

    public static final String FEEDBACK_MODEL_KEY="feedback";
    private static final String CONTACT_US_VIEW_NAME="contact";

    @Autowired
    private EmailService emailService;

    @RequestMapping(value="/contact",method=RequestMethod.GET)
    public String contactGet(ModelMap model){
        FeedBackPojo feedBackPojo=new FeedBackPojo();
        model.addAttribute(ContactController.FEEDBACK_MODEL_KEY, feedBackPojo);
        return ContactController.CONTACT_US_VIEW_NAME;
    }

    @RequestMapping(value="/contact",method=RequestMethod.POST)
    public String contactPost(@ModelAttribute(FEEDBACK_MODEL_KEY) FeedBackPojo feedback){
        LOG.debug("feedback pojo content:{}",feedback);
        emailService.sendFeedBackEmail(feedback);
        return ContactController.CONTACT_US_VIEW_NAME;
    }
}

接口EmailService

package com.proto.backendservice;

import org.springframework.mail.SimpleMailMessage;

import com.proto.domainFrontend.FeedBackPojo;

public interface EmailService {

    public void sendFeedBackEmail(FeedBackPojo feedBackPojo);

    public void sendGenericEmailMessage(SimpleMailMessage message);
}

抽象类AbstractEmailService

package com.proto.backendservice;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.proto.domainFrontend.FeedBackPojo;

@Service
public abstract class AbstractEmailService implements EmailService{

    @Value("${default.to.address}")
    private String defaultToAddress;

    protected SimpleMailMessage prepareSimpleMailMessageFromFeedBackPojo(FeedBackPojo feedback){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setTo(defaultToAddress);
        message.setFrom(feedback.getEmail());
        message.setSubject("[Protoweb:Feedback Received from"+feedback.getFirstName()+" "+feedback.getLastName()+"!");
        message.setText(feedback.getFeedBack());
        return message;
    }

    @Override
    public void sendFeedBackEmail(FeedBackPojo feedBackPojo){
        sendGenericEmailMessage(prepareSimpleMailMessageFromFeedBackPojo(feedBackPojo));
    }
}

类MockMailService

package com.proto.backendservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;


@Service
public class MockMailService extends AbstractEmailService {

    private static final Logger LOG=LoggerFactory.getLogger(MockMailService.class);

    @Override
    public void sendGenericEmailMessage(SimpleMailMessage message) {
        LOG.debug("Simulating an email service....");
        LOG.info(message.toString());
        LOG.debug("Email Sent");
    }


}

application.properties

spring.profile.active=dev

default.to.address=xxx@gmail.com

application-prod.properties

spring.mail.host=smtp.gmail.com
spring.mail.username=xxx@gmail.com
spring.mail.password=xxx    
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=flase
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable=true

0 个答案:

没有答案