【问题标题】:UnsatisfiedDependencyException autowired on constructor and set not workingUnsatisfiedDependencyException 在构造函数上自动装配并设置不工作
【发布时间】:2017-06-26 13:06:55
【问题描述】:

我创建了一个自定义存储库。在这个存储库中,我需要访问标准存储库的一些方法(findAll),所以我添加了

我用的是弹簧靴

@EntityScan(basePackageClasses = {UserAgendaApplication.class, Jsr310JpaConverters.class})
@SpringBootApplication
public class UserAgendaApplication {

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


public interface UsersRepositoryCustom {
    public Page<Users> customSearch(UserSearch UserSearch, Pageable page);
}



public interface UsersRepository extends JpaRepository<Users, Integer>, JpaSpecificationExecutor<Users>, UsersRepositoryCustom {

}


@Repository
public class UsersRepositoryImpl implements UsersRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    private final UsersRepository usersRepository;

   @Autowired
   public UsersRepositoryImpl(final UsersRepository usersRepository){
        this.usersRepository=usersRepository;
   }

   @Override
   public Page<Users> customSearch(UserSearch UserSearch, Pageable page) {
         ...
         return usersRepository.findAll(specification, page);

   }

}

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    public UsersServiceImpl(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

    private UsersRepository usersRepository;

    @Override
public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
   usersRepository.customSearch(userSearch, page);
    ... 
}

}

在一个控制器中

@Controller
public class UsersController {

     private final UsersService usersService;

    @Autowired
    public UsersController( final usersService usersService){
        this.usersService=usersService;
    }

}

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“usersController”的 bean 时出错:不满意 通过字段“usersService”表示的依赖关系;
嵌套异常 是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建文件中定义的名称为“usersServiceImpl”的 bean 时出错 [/home/alpha/Development/project/UserAgenda/build/classes/main/com/alpha/userAgenda/service/UsersServiceImpl.class]: 通过构造函数参数 0 表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException: 创建名称为“usersRepositoryImpl”的 bean 时出错:名称为 Bean 'usersRepositoryImpl' 已注入其他 bean [usersRepository] ​​在其原始版本中作为循环引用的一部分, 但最终被包裹。这意味着所说的其他bean 不使用 bean 的最终版本。这往往是由于 过度渴望类型匹配 - 考虑使用 'getBeanNamesOfType' 例如,'allowEagerInit' 标志已关闭。

我尝试在控制器和 UsersRepositoryImpl 中的 setUsersRepository 上设置自动装配,但得到相同的错误

编辑

public abstract class UsersRepositoryCustom extends SimpleJpaRepository<Users, Integer>

public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
...
}

因为我使用的是 SimpleJpaRepository,所以我不再需要 UserRepository,但我需要在 UersRepositoryCustom 中添加这段代码

private final EntityManager entityManager;
private final JpaEntityInformation<Users, Integer> entityInformation;

@Autowired
public UsersRepositoryCustom(final JpaEntityInformation<Users, Integer> entityInformation,
        final EntityManager entityManager) {
    super(entityInformation, entityManager);
    this.entityManager = entityManager;
    this.entityInformation = entityInformation;
}

在实现UsersService的我的UsersServiceImpl中,我有

@Autowired
public UsersRepositoryCustom usersRepositoryCustom;

我明白了

.UsersRepositoryImpl 中构造函数的参数 0 需要一个无法找到的 'org.springframework.data.jpa.repository.support.JpaEntityInformation' 类型的 bean。

编辑 2

分级配置

buildscript {
    ext {
        springBootVersion = '2.0.0.M2'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

mainClassName = 'com.alpha.useragenda.UserAgendaApplication'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')

    compile group: 'org.hsqldb', name: 'hsqldb', version: '2.0.0'
    compile group: 'org.postgresql', name: 'postgresql', version: '42.1.1'

    compile group: 'org.webjars', name: 'bootstrap', version: '3.3.7'
    compile group: 'org.webjars.bower', name: 'bootstrap-table', version: '1.11.1'
    compile('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

tasks.withType(JavaCompile) {
    options.compilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
}

【问题讨论】:

  • 那永远行不通。要创建UserRepository,您需要一个自定义实现的实例,但该自定义实现需要UserRepository。您对 bean 本身有依赖性... customSearch 方法有什么特别之处,以至于您需要一个特殊的实现而不是常规的 findAll
  • 当您使用自定义存储库时,肯定有一种访问存储库方法的方法
  • 并非没有从 Spring Data 的角度创建循环依赖。所以目前没有。如前所述,customSearch 方法的特别之处在于您需要自定义实现...
  • 如果您需要创建自定义查询,而不是使用 @Query 注释,您需要使用您的方法创建自己的存储库...
  • 你可以添加一个抽象类,将 customSearch 方法移到它上面,让它实现 UsersRepository 接口。这样,您将在调用 repo 的代码中拥有一个抽象接口,并且没有循环依赖。只是一个想法

标签: java spring spring-boot


【解决方案1】:

我认为你最好的方法是使用这个

package stack.overflow;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface UsersCrud extends CrudRepository<Users, Long> {

    //My other query prototypes here

    @Query("select c1 from t1")
    public Object customQuery();
}

通过这种方式,您可以轻松获得特殊查询。否则,我建议遵循 OP cmets 中给出的建议(不理会存储库并处理服务中的特殊情况

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2020-05-23
    • 1970-01-01
    • 2016-09-04
    相关资源
    最近更新 更多