尽管包在组件扫描中,但 Spring Boot 找不到 bean

时间:2021-05-27 06:36:16

标签: java spring-boot

Spring-Boot 没有找到 bean,即使我向 ComponentScan 添加了显式包。它不受配置文件限制,我添加了@Repository 注释。

显示 SpringBoot 无法找到 bean 的异常。

2021-05-27 02:25:39 DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model.CostDataRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1716)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1272)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at com.mycomp.proj.cloud.cost.ssc.file.generator.application.SscFileGeneratorWebApplication.main(SscFileGeneratorWebApplication.java:26)

SscFileGeneratorWebApplication.java

package com.mycomp.proj.cloud.cost.ssc.file.generator.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {
         ErrorMvcAutoConfiguration.class
        })
@ComponentScan({"com.mycomp.proj.cloud.cost.ssc.generator"
,"com.mycomp.proj.cloud.cost.ssc.file.generator.configuration"
,"com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model"
,"com.mycomp.proj.cloud.cost.ssc.file.generator.infrastructure.persistence.file.system"
,"com.mycomp.proj.cloud.cost.ssc.file.generator.application"})
public class SscFileGeneratorWebApplication {

    public static void main(String[] args) {
        // This is to fix the reloader bug in DevTools of Spring Boot
        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(SscFileGeneratorWebApplication.class, args);
    }

}

CostDataRepository.java

package com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model;

import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CostDataRepository extends CrudRepository<CostData, Long> {

    List<CostData> findByCostDataIdGreaterThanOrderByCostDataIdAsc(Long lastId);
}

生成文件服务.java

package com.mycomp.proj.cloud.cost.ssc.file.generator.application;

import java.util.List;
import com.mycomp.proj.cloud.cost.ssc.file.generator.application.util.LogLayer;
import com.mycomp.proj.cloud.cost.ssc.file.generator.application.util.LogType;
import com.mycomp.proj.cloud.cost.ssc.file.generator.application.util.LogUtils;
import com.mycomp.proj.cloud.cost.ssc.file.generator.configuration.FileGeneratorProperties;
import com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model.CostData;
import com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model.CostDataRepository;
import com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model.SSCFileDescriptor;
import com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model.csv.CsvFileGenerator;
import com.mycomp.proj.cloud.cost.ssc.file.generator.domain.model.ebcdic.EBCDICFileGenerator;
import com.mycomp.proj.cloud.cost.ssc.file.generator.infrastructure.persistence.file.system.FileWriter;
import com.mycomp.proj.cloud.cost.ssc.file.generator.infrastructure.port.email.EmailService;
import org.slf4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mycomp.proj.cloud.cost.ssc.file.generator.process.model.LastRunResult;
import com.mycomp.proj.cloud.cost.ssc.file.generator.process.model.RunResult;
import com.mycomp.proj.cloud.cost.ssc.file.generator.process.model.RunResultService;

@Service
public class GenerateFileService {
    private CostDataRepository costDataRepository;
    private FileWriter fileWriter;
    private final FileGeneratorProperties properties;
    private static final String CREATE_FILES = "CREATE_FILES";
    private Logger logger;
    private final ObjectFactory<SSCFileDescriptor> fileDescriptorFactory;
    private BeanFactory beanFactory;
    private RunResultService runResultService;
    private EmailService emailService;

    @Autowired
    public GenerateFileService(CostDataRepository costDataRepository, FileWriter fileWriter, FileGeneratorProperties properties,
                               ObjectFactory<SSCFileDescriptor> fileDescriptorFactory,
                               BeanFactory beanFactory, RunResultService runResultService,
                               EmailService emailService) {
        this.costDataRepository = costDataRepository;
        this.fileWriter = fileWriter;
        this.properties = properties;
        this.fileDescriptorFactory = fileDescriptorFactory;
        this.beanFactory = beanFactory;
        this.runResultService = runResultService;
        this.emailService = emailService;
    }
    ...


}

0 个答案:

没有答案
相关问题