我有理由相信SpringBoot至少不会扫描实体类,尽管实体位于主包的子包中。我让它与@ComponentScan注释一起工作,至少在某种程度上是这样。但是,我注意到另一个组件也没有运行。我相信这是一件非常简单的事情,因为这是一份来自不同软件包的非常接近的副本。
注意:我尝试添加@ComponentScan,@EntityScan,@EnableJpaRepository。这已经摆脱了最初的错误,但是它没有拾取@Service GenerateFileService(如下所示),所以我将相应的包添加到所有注释中。我原以为@SpringBootApplication会自动找到所有相关的Spring bean,前提是所有类都在Application的子包中,但事实并非如此。
堆栈跟踪是:
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.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);
}
这项工作是现有项目的一个副本,但我需要对其进行重构,以便部署到云平台。我希望得到一些指导,比如说你遗漏了@SpringFinder注释,或者类似的注释,或者某个注释遗漏了。相反,我放弃了尝试,重新开始。有效的版本只有@springboot应用程序,尽管我不知道它不喜欢什么。启动一个新项目有帮助。
从错误日志中,您定义了管理实体com.mycomp.cloud.cost.ssc.file.generator.domain.model.CostData
的存储库CostDataRepository,但是包com.mycomp.cloud.cost.ssc.file.generator.domain
不在您配置的@EntityScan
中。
另外,@ComponentScan
用于组件而不是实体,因此您应该删除此行。
我对Spring bean扫描有点困惑。 AFAIK我需要把
我正在使用一个带有spring boot 2.0.0.rc1的多项目分级器。我的子项目之一是SpringBoot应用程序,其中包含了我的集成测试。 集成测试用WebEnvironment.random_port标记为@springboottest。由于未解析的依赖关系(在另一个子项目中声明的服务,的同级),测试失败,使用了gradle命令行,但在Eclipse IDE中成功。 如果有人有主意?如何
本文向大家介绍详解spring自动扫描包,包括了详解spring自动扫描包的使用技巧和注意事项,需要的朋友参考一下 配置文件 前面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用XML的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Co
问题内容: 我的设置: Sonarqube 5.1.1 Sonar-Maven插件2.6(也尝试过2.7和3.6) JDK 1.7.0_51 错误示例: 根据这个问题,javax.annotation应该是Java1.7及更高版本的一部分。此外,我尝试将其放在本地Maven存储库中,但没有帮助。 那么,Sonar试图在哪里找到这个软件包?有帮助吗?!? 更新: 我试过修改sonar-maven-p
在spring boot应用程序中,我有一个包含应用程序类包,如 默认情况下,它自动从该类的包中设置ComponentScan。然后我有几个子包,每个子包包含几个组件和服务bean(使用注释)。但是为了对不同的用例重用这个应用程序,我需要启用/禁用一些子包中的所有组件,最好是通过一个属性。 那就是我有一些子包,比如 现在,基于某些属性,我想在其中一个包中启用(扫描)bean,例如。 我以为我可以使
我的设置: 这可能是5.1。1 Sonar Maven插件2.6(也尝试了2.7和3.6) jdk1.7。0_51 错误示例: 根据这个stackoverflow问题,javax.annotation应该是java 1.7及以上版本的一部分。此外,我试图把它放在本地的maven存储库,但没有帮助。 声纳在哪里找到这个包裹?有人帮忙吗?!? 更新: 我尝试修改声纳maven-plugin以包含对ja