spring引导不扫描主包的子包

jtjikinw  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(436)

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

生成文件服务:

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.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);
}
2sbarzqh

2sbarzqh1#

从错误日志中,您正在定义管理实体的存储库costdatarepository com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData 但是这个包裹 com.mycomp.cloud.cost.ssc.file.generator.domain 没有出现在 @EntityScan 您正在配置。
@ComponentScan 用于组件而不是实体,因此应删除此行。

eyh26e7m

eyh26e7m2#

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

相关问题