java - 移动配置、实体和其他类

标签 java spring-boot spring-data-jpa

我有一个大问题。 我有一个带有 Spring-Boot 和 Spring-Data 的应用程序。 在我移动一些类并重构目录之前一切进展顺利。

我有两个数据库,在移动类之前,我有第二个数据库的配置类:

         @Configuration
      @EnableTransactionManagement
      @EnableJpaRepositories(
         basePackages="it.phoops.rt.grigliaprocessi.util", 
         entityManagerFactoryRef = "organoEntityManager",
         transactionManagerRef = "organoTransactionManager")     
      public class OrganoConfig {

          @Bean(name = "organoDataSource")
           @ConfigurationProperties(prefix="organo.datasource")
          public DataSource organoDataSource(){
                  return DataSourceBuilder.create().build();
           }

          @Bean(name="organoEntityManager")
          public LocalContainerEntityManagerFactoryBean    
       organoEntityManagerFactory(EntityManagerFactoryBuilder builder,
         @Qualifier("organoDataSource") DataSource dataSource){

          return       
 builder.
 dataSource(organoDataSource()).
 packages("it.phoops.rt.grigliaprocessi.util.entity").
  persistenceUnit("organo").build();
 }

           @Bean("organoTransactionManager")

                public PlatformTransactionManager 
 organoTransactionManager()               
 {     
                      return new   
  DataSourceTransactionManager(organoDataSource());
        }

 }

现在,我搬进了“it.phoops.rt.grigliaprocessi.organo”,并将“basePackages”的值从“it.phoops.rt.grigliaprocessi.util”更改为“it.phoops.rt.grigliaprocessi”。有机”和
字符串“it.phoops.rt.grigliaprocessi.util.entity”到 it.phoops.rt.grigliaprocessi.organo.entity”。

我将实体、服务和存储库类移至:

it.phoops.rt.grigliaprocessi.util.controller
it.phoops.rt.grigliaprocessi.util.entity
it.phoops.rt.grigliaprocessi.util.repository

it.phoops.rt.grigliaprocessi.organo.controller
it.phoops.rt.grigliaprocessi.organo.entity
it.phoops.rt.grigliaprocessi.organo.repository

但是现在,我遇到了这个错误,是由嵌套异常引起的:

Caused by: java.lang.IllegalArgumentException: Not a managed type: 
class it.phoops.rt.grigliaprocessi.organo.entity.Albero

……

 Caused by: org.springframework.beans.factory.BeanCreationException:   
 Error creating bean with name 'alberoRepository': Invocation of init 
 method failed; nested exception is 
 java.lang.IllegalArgumentException: 
 Not a managed type: class 
 it.phoops.rt.grigliaprocessi.organo.entity.Albero

...

Caused by: 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error 
creating bean with name 'alberoService': Unsatisfied dependency 
expressed through field 'repo'; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error 
creating 
bean with name 'alberoRepository': Invocation of init method failed; 
nested exception is java.lang.IllegalArgumentException: Not a managed 
type: class it.phoops.rt.grigliaprocessi.organo.entity.Albero

...

Caused by: 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error 
creating bean with name 'alberoController': Unsatisfied dependency 
expressed through field 'alberoService'; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error 
creating bean with name 'alberoService': Unsatisfied dependency 
expressed through field 'repo'; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error 
creating 
bean with name 'alberoRepository': Invocation of init method failed; 
nested exception is java.lang.IllegalArgumentException: Not a managed 
type: class it.phoops.rt.grigliaprocessi.organo.entity.Albero

你能帮我吗?

谢谢

最佳答案

将基础包声明从 it.phoops.rt.grigliaprocessi.util 更改为 it.phoops.rt.grigliaprocessi.organo。另请检查 Spring boot 主类中的 componentScan 中的基础包。

关于java - 移动配置、实体和其他类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57875079/

相关文章:

java - 为什么 jvm 命令 `jinfo` 在 alpine openjdk8 发行版中没有 `-flags`?

java - 检索 log4j 的 Activity 日志记录级别

java - 将具有数百万条记录的表从一个数据库复制到另一个数据库 - Spring Boot + Spring JDBC

java - 不能对 JpaSpecificationExecutor<T> 存储库使用 findAll(Specifications<T>, Pageable) 方法?

java - Spring 启动JPA : Use library to Bulk load data for MySQL

java - 使用简单的 spring-data-rest 项目获取 404 错误

java - 关于java中线程的wait()和notify()方法

java - 在 Spring Boot 测试中获取配置属性类 bean

java - 如何更新对象 hibernate

java - LocalContainerEntityManagerFactoryBean 无法转换为 EntityManagerFactory