[英]Is it wrong to @Inject the repository in a custom repository (Spring Data JPA)
[英]How to inject configuration in a custom Spring Data JPA repository
我想基于Hibernate和Spring Data的JPA批处理插入中的代码向我的应用程序中的所有Spring Data JPA存储库添加自定义的批量保存方法。 Spring Data文档解释了如何使用自定义存储库基类(如以下所示) 来完成此操作 。 我的问题是如何在下面的示例中设置batchSize
属性。 如下所示,使用@Value进行注入无效。
@NoRepositoryBean
public interface BulkRepository<T, ID extends Serializable>
extends JpaRepository<T, ID> {
void bulkSave(Iterable<T> entities);
}
public class BulkRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BulkRepository<T, ID> {
private final EntityManager entityManager;
@Value("${spring.jpa.properties.hibernate.jdbc.batch_size}")
private int batchSize;
public BulkRepositoryImpl(JpaEntityInformation entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
public void bulkSave(Iterable<T> entities) {
// implementation using batchSize goes here
}
}
我是否必须使用自定义JpaRepositoryFactoryBean
构建已配置的BulkRepositoryImpl
? 还是有更直接的方法?
我遇到了完全相同的问题,找不到定义我自己的JpaRepositoryFactoryBean
类的任何方法。 似乎自定义存储库基类的依赖项并没有像在标准Bean中那样自动注入(请参阅here和here )。 另外,在创建存储库接口的实例时,默认的JpaRepositoryFactory
仅将JpaEntityInformation
和EntityManager
实例传递给类构造函数(请参见此处 )。 据我所知,这有效地阻止了您为扩展SimpleJpaRepository
类添加其他依赖SimpleJpaRepository
。
我最终通过以下方式定义了自定义工厂:
@Configuration
@ConfigurationProperties(prefix = "spring.jpa.properties.hibernate.jdbc")
public class RepositoryConfiguration {
private int batchSize;
}
public class MyCustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> {
private RepositoryConfiguration repositoryConfiguration;
public MyCustomRepositoryFactoryBean(Class<? extends R> repositoryInterface, RepositoryConfiguration repositoryConfiguration) {
super(repositoryInterface);
this.repositoryConfiguration = repositoryConfiguration;
}
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new MyCustomRepositoryFactory(entityManager, repositoryConfiguration);
}
private static class MyCustomRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
private RepositoryConfiguration repositoryConfiguration;
MyCustomRepositoryFactory(EntityManager entityManager, RepositoryConfiguration repositoryConfiguration) {
super(entityManager);
this.repositoryConfiguration = repositoryConfiguration;
}
@Override
@SuppressWarnings("unchecked")
protected SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information,
EntityManager entityManager) {
JpaEntityInformation<T, ?> entityInformation =
(JpaEntityInformation<T, ?>) getEntityInformation(information.getDomainType());
return new MyCustomRepositoryImpl<T, I>(
entityInformation,
entityManager,
repositoryConfiguration);
}
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return MyCustomRepositoryImpl.class;
}
}
}
虽然也无法在MyCustomRepositoryFactoryBean
使用@Value
注入字段, @Value
Spring解析了传递给构造函数的依赖项,因此您可以通过Bean(上面的代码中的RepositoryConfiguration
)提供属性,然后将其传递给MyCustomRepositoryImpl
。 最后,您需要通过添加以下内容来指示Spring Data在创建存储库时使用FactoryBean
类:
@EnableJpaRepositories(
repositoryFactoryBeanClass = MyCustomRepositoryFactoryBean.class
)
到@Configuration
注释的bean。 这是很多样板,但有效。
注意我正在使用spring-data-jpa:1.11.8.RELEASE
。
您可以从EntityManager中获取它。
String defaultBatchSize = "100";
String batchSizeString = (String) entityManager.getEntityManagerFactory().getProperties()
.getOrDefault("hibernate.jdbc.batch_size", defaultBatchSize);
batchSize = Integer.parseInt(batchSizeString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.