注入依赖性Spring Boot 2

时间:2018-12-13 16:58:38

标签: spring-boot

我正在使用Spring Cloud做一些练习,尝试启动时我的应用程序给出了一个错误:


申请无法开始


说明:

Parameter 2 of constructor in com.innovation.validadorcpf.core.service.impl.CPFServiceImpl required a bean of type 'com.innovation.validadorcpf.core.repository.CPFRepository' that could not be found.

操作:

考虑在您的配置中定义类型为com.innovation.validadorcpf.core.repository.CPFRepository的bean。

以退出代码1完成的过程

这是我的服务等级,我找不到错误,有人可以帮助我吗?

package com.innovation.validadorcpf.core.service;

import com.innovation.validadorcpf.core.model.CPF;

import java.util.List;

public interface CPFService {
    String validaCPF(String numeroCPF);
    CPF cadastrarCPF(String numeroCPF);
    List<CPF> listagemCPF();
}

package com.innovation.validadorcpf.core.service.impl;

import com.innovation.validadorcpf.core.util.Message;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.innovation.validadorcpf.ws.exception.ValidadorCPFException;
import com.innovation.validadorcpf.core.model.CPF;
import com.innovation.validadorcpf.core.repository.CPFRepository;
import com.innovation.validadorcpf.core.service.CPFService;
import com.innovation.validadorcpf.core.util.converter.StringToCPFConverter;
import com.innovation.validadorcpf.core.util.helper.MessageHelper;
import com.innovation.validadorcpf.core.util.validator.CPFValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
public class CPFServiceImpl implements CPFService {

    private Message mensagem;
    private CPFValidator cpfValidator;
    private CPFRepository cpfRepository;
    private StringToCPFConverter cpfConverter;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public CPFServiceImpl(Message mensagem, CPFValidator cpfValidator, CPFRepository cpfRepository, StringToCPFConverter cpfConverter) {
        this.mensagem = mensagem;
        this.cpfValidator = cpfValidator;
        this.cpfRepository = cpfRepository;
        this.cpfConverter = cpfConverter;
    }

    @Override
    public String validaCPF(String numeroCPF) {
        if (ObjectUtils.isEmpty(numeroCPF)) {
            String mensagemErroI18n = mensagem.getMessage(MessageHelper.CPF_VAZIO, numeroCPF);
            logger.error(mensagem.getMessage(MessageHelper.CPF_VALIDAR_ERRO, mensagemErroI18n));
            throw new ValidadorCPFException(mensagem.getMessage(MessageHelper.CPF_VALIDAR_ERRO, mensagemErroI18n));
        }
        if (cpfValidator.validarCPF(numeroCPF)) {
            logger.debug(mensagem.getMessage(MessageHelper.CPF_VALIDO, numeroCPF));
            return mensagem.getMessage(MessageHelper.CPF_VALIDO, numeroCPF);
        } else {
            logger.debug(mensagem.getMessage(MessageHelper.CPF_INVALIDO, numeroCPF));
            return mensagem.getMessage(MessageHelper.CPF_INVALIDO, numeroCPF);
        }
    }

    @Override
    public CPF cadastrarCPF(String numeroCPF) {
        numeroCPF = numeroCPF.replaceAll("[^0-9]", "");
        if (!cpfValidator.validarCPF(numeroCPF)) {
            String mensagemErroI18n = mensagem.getMessage(MessageHelper.CPF_INVALIDO, numeroCPF);
            logger.error(mensagem.getMessage(MessageHelper.CPF_CADASTRAR_ERRO, mensagemErroI18n));
            throw new ValidadorCPFException(mensagem.getMessage(MessageHelper.CPF_CADASTRAR_ERRO, mensagemErroI18n));
        }
        if (!verificaSeCPFPossuiCadastro(numeroCPF)) {
            String mensagemErroI18n = mensagem.getMessage(MessageHelper.CPF_DUPLICADO, numeroCPF);
            logger.error(mensagem.getMessage(MessageHelper.CPF_CADASTRAR_ERRO, mensagemErroI18n));
            throw new ValidadorCPFException(mensagem.getMessage(MessageHelper.CPF_CADASTRAR_ERRO, mensagemErroI18n));
        } else {
            return cpfRepository.insert(cpfConverter.convert(numeroCPF));
        }
    }

    @Override
    public List<CPF> listagemCPF() {
        return cpfRepository.findAll();
    }

    private Boolean verificaSeCPFPossuiCadastro(String numeroCPF) {
        CPF cpfCadastrado = cpfRepository.findCPFByNumero(numeroCPF);
        return ObjectUtils.isEmpty(cpfCadastrado);
    }

}

package com.innovation.validadorcpf.core.repository;

import com.innovation.validadorcpf.core.model.CPF;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CPFRepository extends MongoRepository <CPF, String> {

    CPF findCPFById();
    CPF findCPFByNumero(String numeroCPF);
}

package com.innovation.validadorcpf;

import com.innovation.validadorcpf.ws.config.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(
                ModuleConfiguration.class,
                Application.class,
                EurekaClientConfiguration.class,
                MongoConfiguration.class,
                JacksonConfiguration.class,
                SwaggerConfiguration.class
        ).run(args);
    }
}

日志:

2018-12-13 15:22:14.074 DEBUG 23944 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@76ed1b7c, started on Thu Dec 13 15:22:03 BRST 2018
2018-12-13 15:22:14.074 DEBUG 23944 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String
2018-12-13 15:22:14.540 DEBUG 23944 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : Application failed to start due to an exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.innovation.validadorcpf.core.repository.CPFRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:855) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:758) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at com.innovation.validadorcpf.Application.main(Application.java:18) [classes/:na]

2018-12-13 15:22:14.547 ERROR 23944 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 2 of constructor in com.innovation.validadorcpf.core.service.impl.CPFServiceImpl required a bean of type 'com.innovation.validadorcpf.core.repository.CPFRepository' that could not be found.


Action:

Consider defining a bean of type 'com.innovation.validadorcpf.core.repository.CPFRepository' in your configuration.


Process finished with exit code 1

谢谢。

2 个答案:

答案 0 :(得分:0)

CPFServiceImpl类中进行自动接线,如下所示:

@Autowired
private CPFRepository cpfRepository;

对构造函数中定义的所有变量都执行相同的操作,除非在其他地方未设置该变量,否则不需要此构造函数:

 @Autowired
    public CPFServiceImpl(Message mensagem, CPFValidator cpfValidator, CPFRepository cpfRepository, StringToCPFConverter cpfConverter) {
        this.mensagem = mensagem;
        this.cpfValidator = cpfValidator;
        this.cpfRepository = cpfRepository;
        this.cpfConverter = cpfConverter;
    }

并定义以下类(可选):

@Slf4j
@Service("cPFService")
public class CPFServiceImpl implements CPFService {

答案 1 :(得分:0)

@cahen

是的,在我的组件扫描和MongoConfiguration类中,我指定了其他程序包