我正在尝试手动定义自定义Spring Data Repository,我有以下3个类:
public interface PersonRepository extends JpaRepository<Person, Long>...
public interface PersonRepositoryCustom
public class PersonRepositoryImpl implements PersonRepositoryCustom {
@Resource
private PersonRepository personRepository;
......
}
为了在配置类中进行配置,我有以下内容:
要在 Configuration 类中进行配置,我有以下内容:
@Bean
public PersonRepositoryImpl personRepositoryImpl() {
return new PersonRepositoryImpl();
}
@Bean
public PersonRepository personRepository() {
return getFactoryBean(PersonRepository.class, personRepositoryImpl());
}
private <T> T getFactoryBean(Class<T> repository, Object customImplementation) {
BaseRepositoryFactoryBean factory = new BaseRepositoryFactoryBean();
factory.setEntityManager(entityManager);
factory.setBeanFactory(beanFactory);
factory.setRepositoryInterface(repository);
if (customImplementation != null) {
factory.setCustomImplementation(customImplementation);
}
factory.afterPropertiesSet();
return (T) factory.getObject();
}
我遇到的问题是出现了“创建名为‘personRepository’的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用”错误。
这似乎是因为PersonRepositoryImpl包含对personRepository接口的资源引用。
如果我在配置类上使用EnableJpaRepositories注释,那么一切似乎都能正常工作。然而,我不想使用那个注释,它是基于包进行扫描的,我希望有更精细的可配置性。
那么有人知道如何手动设置一个Spring自定义存储库,以允许注入而不出现循环引用问题吗?
有人知道吗?