gpt4 book ai didi

Spring Data JPA 自定义通用存储库模式问题

转载 作者:行者123 更新时间:2023-12-01 08:58:21 25 4
gpt4 key购买 nike

我在 Spring 中有以下配置,但由于存储库的 Impl 类中缺少 init 方法, Autowiring 失败。 Spring 不应该尝试通过构造函数来初始化 bean,但它应该使用 Factory……我错过了一些简单的配置……或者我遇到了错误。

我正在尝试实现一个单一的通用存储库,其中所有存储库都可以共享方法和特定于我的映射域类的特定方法...

这是我的错误:

Error creating bean with name 'auditRepositoryImpl' defined in file AuditRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.domain.biz.dao.impl.AuditRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.domain.biz.dao.impl.AuditRepositoryImpl.<init>()

另一方面,我的 CustomFactory 似乎没有被取走。

2014-07-05 08:16:48,343 DEBUG  org.springframework.data.repository.config.RepositoryComponentProvider Identified candidate component class: file [InventoryRepository.class] 

...

2014-07-05 08:16:48,366 DEBUG  org.springframework.data.repository.config.RepositoryBeanDefinitionBuilder Registering custom repository implementation: auditRepositoryImpl AuditRepositoryImpl 
2014-07-05 08:16:48,367 DEBUG org.springframework.data.repository.config.RepositoryConfigurationDelegate Registering repository: auditRepository - Interface: AuditRepository - Factory: org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean







//Spring Java config
@Configuration
@EnableScheduling
@EnableSpringConfigured
@Import(EnvConfiguration.class)
@EnableAspectJAutoProxy
@EnableJpaRepositories(repositoryFactoryBeanClass = DefaultRepositoryFactoryBean.class, basePackages = { "com.domain.biz.dao" }, repositoryImplementationPostfix = "Impl")
@EnableCaching
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan(basePackages = { "com.domain.biz" })
@Order(2)
public class AppConfiguration extends CachingConfigurerSupport implements LoadTimeWeavingConfigurer

...

@NoRepositoryBean
public interface GenericRepository<T extends Serializable, I extends Serializable>
extends JpaRepository<T, I> {

...


@NoRepositoryBean
public abstract class AbstractRepositoryImpl<T extends Serializable, I extends Serializable>
extends SimpleJpaRepository<T, I> implements GenericRepository<T, I> {

private static Logger log = LoggerFactory
.getLogger(AbstractRepositoryImpl.class);

private Class<T> clazz;

@Autowired
EntityManager entityManager;

@Autowired
SessionFactory sessionFactory;

public AbstractRepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);

}

public AbstractRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);

}


...


@NoRepositoryBean
// @Scope( BeanDefinition.SCOPE_PROTOTYPE )
public class GenericRepositoryImpl<T extends Serializable, I extends Serializable>
extends AbstractRepositoryImpl<T, I> implements GenericRepository<T, I> {


...

public interface AuditRepositoryCustom {

public Audit audit(Audit audit);



public interface AuditRepository extends GenericRepository<Audit, Long>, AuditRepositoryCustom {




public class DefaultRepositoryFactoryBean<R extends JpaRepository<T, I>, T extends Serializable, I extends Serializable>
extends JpaRepositoryFactoryBean<R, T, I> {

private static class RepositoryFactory<T extends Serializable, I extends Serializable>
extends JpaRepositoryFactory {

private EntityManager entityManager;

public RepositoryFactory(EntityManager entityManager) {
super(entityManager);

this.entityManager = entityManager;
}

@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

// The RepositoryMetadata can be safely ignored, it is used by the
// JpaRepositoryFactory
// to check for QueryDslJpaRepository's which is out of scope.
return GenericRepository.class;
}

@Override
protected Object getTargetRepository(RepositoryMetadata metadata) {

return new GenericRepositoryImpl<T, I>(
(Class<T>) metadata.getDomainType(), this.entityManager);
}
}

@Override
protected RepositoryFactorySupport createRepositoryFactory(
EntityManager entityManager) {

return new RepositoryFactory(entityManager);
}

最佳答案

异常的根本原因非常清楚。您的 AuditRepositoryImpl 没有无参数构造函数或使用 @Inject/@Autowired 注释的构造函数。

关于Spring Data JPA 自定义通用存储库模式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com