Java Spring boot未扫描主包的子包

Java Spring boot未扫描主包的子包,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我有理由相信SpringBoot至少不会扫描实体类,尽管实体位于主包的子包中。我让它与@ComponentScan注释一起工作,至少在某种程度上是这样。但是,我注意到另一个组件也没有运行。我相信这是一件非常简单的事情,因为这是一份来自不同软件包的非常接近的副本 注意:我尝试添加@ComponentScan、@EntityScan、@EnableJpaRepositories。这已经消除了最初的错误,但它没有拾取@Service GenerateFileService(如下所示),因此我将相应的包

我有理由相信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

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


}
CostDataRepository.java

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);
}
包com.mycop.cloud.cost.ssc.file.generator.domain.model;
导入java.util.List;
导入org.springframework.data.repository.crudepository;
公共接口CostDataRepository扩展了Crudepository{
列出FindBycostDataId大于OrderByCostDataId(长lastId);
}

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


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

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

放弃所有注释,只留下一个普通的
@springbootplication
。假设您的类位于
com.mycomp.cloud.cost.ssc.file.generator
包中,那么它就可以工作了。您是否也阅读了该错误,问题似乎是您在Spring Data JPA存储库中使用时所引用的域类不是
@实体。@M.Deinum刚刚尝试在SpringBootApplication plain上注释所有注释,但得到了相同的错误。应用程序成本sscfilegenerator应用程序位于com.mycop.cloud.cost.ssc.file.generator中。成本数据位于com.mycop.cloud.cost.ssc.file.generator.domain.model中。由于注释不可读,请不要添加其他代码/错误。该错误明确指出,
CostData
不是实体或至少不是JPA管理的类型。所以你一定忘了那个类的注释。您添加的所有注释都解决了错误的问题,甚至可能使事情变得更糟。此外,您的简化和模糊代码使您无法查看包是否正确。一些州
com.ibm.
其他州
com.mycompany
com.mycomp
,因此无法确定哪一项权利。@M.Deinum CostData上有at-javax.persistence.Entity注释。我还缺什么?比如一个
@Id
。基本上,除了
@SpringBootApplication
注释类之外,您在这里发布的所有类对这个问题都不重要。唯一需要做的是存储库和实体。使用正确的包名,这样就可以确定它们的应用程序结构是否正确。我将EntityScan更改为EntityScan({“com.mycomp.cloud.cost.ssc.file.generator.domain.model”、“com.mycomp.cloud.cost.ssc.file.generator.application”、“com.mycomp.cloud.cost.ssc.file.generator.infrastructure.persistence”,“com.mycomp.cloud.cost.ssc.file.generator.infrastructure.persistence.file.system”、“com.mycomp.cloud.cost.ssc.file.generator.configuration”})我有ComponentScan,因为我正在尝试运行一个服务。我认为在我能够完成正确的操作之前,overscan的成本不会太高。应用程序工作吗?它不会启动,因为它找不到CostData对象。如果你正确地使用
@EntityScan
@EnableJpaRepositories
,它应该会工作k、 在(更改包名)之前是否进行过重构?如果是,请尝试清理项目。或者可能是您有两个同名的类,但在CostDataRepository中放置了错误的类。我执行了一个Eclipse项目->清理,但仍然失败。
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;
    }


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