有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导未扫描主包的子包

我有理由相信SpringBoot至少不会扫描实体类,尽管实体位于主包的子包中。我让它与@ComponentScan注释一起工作,至少在某种程度上是这样。但是,我注意到另一个组件也没有运行。我相信这是一件非常简单的事情,因为这是一份来自不同软件包的非常接近的副本

注意:我尝试添加@ComponentScan、@EntityScan、@EnableJpaRepositories。这已经消除了最初的错误,但它没有拾取@Service GenerateFileService(如下所示),因此我将相应的包添加到所有注释中。我原以为@SpringBootApplication会自动找到所有相关的SpringBean,前提是所有类都在应用程序的子包下,但事实并非如此

堆栈跟踪是:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-19 06:28:48 ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'generateFileService' defined in file [C:\projects-new\cirrus-bluecost-ssc-file-generator\target\classes\com\mycomp\cloud\cost\ssc\file\generator\application\GenerateFileService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'costDataRepository' defined in com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostDataRepository defined in @EnableJpaRepositories declared on BlueCostSSCFileGeneratorApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
        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.cloud.cost.ssc.file.generator.BlueCostSSCFileGeneratorApplication.main(BlueCostSSCFileGeneratorApplication.java:26)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'costDataRepository' defined in com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostDataRepository defined in @EnableJpaRepositories declared on BlueCostSSCFileGeneratorApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
        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.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1306)
        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)
        ... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData
        at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582)
        at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85)
        at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75)
        at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:229)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:179)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:162)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:72)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:309)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297)
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:212)
        at org.springframework.data.util.Lazy.get(Lazy.java:94)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300)
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
        ... 30 common frames omitted

主要用途:

package com.mycompany.cloud.cost.ssc.file.generator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@SpringBootApplication(scanBasePackages = "com.ibm.cio.cloud.cost.ssc.file.generator")
@ComponentScan({"com.ibm.cio.cloud.cost.ssc.file.generator.domain.model","com.ibm.cio.cloud.cost.ssc.file.generator.application","com.ibm.cio.cloud.cost.ssc.file.generator.infrastructure.persistence","com.ibm.cio.cloud.cost.ssc.file.generator.infrastructure.persistence.file.system","com.ibm.cio.cloud.cost.ssc.file.generator.configuration"}) /* ,"com.ibm.cio.cloud.cost.ssc.file.generator.domain.model.CostData" */
@EntityScan({"com.ibm.cio.cloud.cost.ssc.file.generator.domain.model","com.ibm.cio.cloud.cost.ssc.file.generator.application","com.ibm.cio.cloud.cost.ssc.file.generator.infrastructure.persistence","com.ibm.cio.cloud.cost.ssc.file.generator.infrastructure.persistence.file.system","com.ibm.cio.cloud.cost.ssc.file.generator.configuration"}) /* ,"com.ibm.cio.cloud.cost.ssc.file.generator.domain.model.CostData" */
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = {"com.ibm.cio.cloud.cost.ssc.file.generator.domain.model","com.ibm.cio.cloud.cost.ssc.file.generator.application","com.ibm.cio.cloud.cost.ssc.file.generator.infrastructure.persistence","com.ibm.cio.cloud.cost.ssc.file.generator.einfrastructure.persistence.file.system","com.ibm.cio.cloud.cost.ssc.file.generator.configuration"},
basePackageClasses = com.ibm.cio.cloud.cost.ssc.file.generator.domain.model.CostDataRepository.class)
public class BlueCostSSCFileGeneratorApplication {

    public static void main(String[] args) {
        
        SpringApplication.run(BlueCostSSCFileGeneratorApplication.class, args);
    }
}


GenerateFileService:

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

import java.util.List;
import com.mycomp.cloud.cost.ssc.file.generator.application.util.LogLayer;
import com.mycomp.cloud.cost.ssc.file.generator.application.util.LogType;
import com.mycomp.cloud.cost.ssc.file.generator.application.util.LogUtils;
import com.mycomp.cloud.cost.ssc.file.generator.configuration.FileGeneratorProperties;
import com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData;
import com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostDataRepository;
import com.mycomp.cloud.cost.ssc.file.generator.domain.model.SSCFileDescriptor;
import com.mycomp.cloud.cost.ssc.file.generator.domain.model.csv.CsvFileGenerator;
import com.mycomp.cloud.cost.ssc.file.generator.domain.model.ebcdic.EBCDICFileGenerator;
import com.mycomp.cloud.cost.ssc.file.generator.infrastructure.persistence.file.system.FileWriter;
import com.mycomp.cloud.cost.ssc.file.generator.infrastructure.port.email.EmailService;
import com.mycomp.cloud.cost.ssc.file.generator.process.model.LastRunResult;
import com.mycomp.cloud.cost.ssc.file.generator.process.model.RunResult;
import com.mycomp.cloud.cost.ssc.file.generator.process.model.RunResultService;

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;

@Service
public class GenerateFileService {

    @Autowired
    private final 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;

    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;
    }

    @Autowired
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    @Transactional
    public FileGenerationResult createFiles() {
        /* Code omitted */
        return result;
    }


}

成本数据实体

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.math.BigDecimal;

@Entity
public class CostData {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long costDataId;
    @Column(name = "COSTINGAMOUNT")
    private BigDecimal totalChargesAmount;


    public Long getCostDataId() {
        return costDataId;
    }


}

CostDataRepository。爪哇

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

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

public interface CostDataRepository extends CrudRepository<CostData, Long> {

    List<CostData> findByCostDataIdGreaterThanOrderByCostDataIdAsc(Long lastId);
}

共 (2) 个答案

  1. # 1 楼答案

    从错误日志中,您正在定义用于管理实体com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData的存储库CostDataRepository,但是包com.mycomp.cloud.cost.ssc.file.generator.domain不在您正在配置的@EntityScan

    另外@ComponentScan用于组件而不是实体,因此您应该删除这一行

  2. # 2 楼答案

    这项工作是现有项目的一个副本,但我需要对其进行重构,以便部署到云平台。我希望得到一些指导,比如说你遗漏了@SpringFinder注释,或者类似的注释,或者某个注释遗漏了。相反,我放弃了尝试,重新开始。有效的版本只有@springboot应用程序,尽管我不知道它不喜欢什么。启动一个新项目有帮助