Java 如何在自定义Spring数据JPA存储库中注入配置
我想在我的应用程序中添加一个自定义的批量保存方法,该方法基于来自所有Spring数据JPA存储库的代码。具有如下所示的自定义存储库基类的。我的问题是如何设置下例中的Java 如何在自定义Spring数据JPA存储库中注入配置,java,spring,repository,spring-data-jpa,Java,Spring,Repository,Spring Data Jpa,我想在我的应用程序中添加一个自定义的批量保存方法,该方法基于来自所有Spring数据JPA存储库的代码。具有如下所示的自定义存储库基类的。我的问题是如何设置下例中的batchSize属性。如下所示的@Value注入不起作用 @NoRepositoryBean public interface BulkRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { void bulkSav
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
}
}
@NoRepositoryBean
公共接口存储库
扩展JPA假设{
void bulkSave(可编辑实体);
}
公共类BulkRepositoryImpl扩展了SimpleParepository实现BulkRepository{
私人最终实体管理人实体管理人;
@值(${spring.jpa.properties.hibernate.jdbc.batch_size})
私有整数批量大小;
public BulkRepositoryImpl(JPA实体信息实体信息,
实体管理器(实体管理器){
超级(实体信息、实体管理器);
this.entityManager=entityManager;
}
公共实体(Iterable实体){
//使用batchSize的实现如下
}
}
我是否必须使用自定义的
JpaRepositoryFactoryBean
来构建已配置的BulkRepositoryImpl
?或者有更直接的方法吗?我遇到了完全相同的问题,无法找到任何方法来定义自己的JpaRepositoryFactoryBean
类。似乎定制存储库基类的依赖项并不像在标准bean中那样自动注入(请参阅和)。另外,在创建存储库接口实例时,默认的JpaRepositoryFactory
仅将JpaEntityInformation
和EntityManager
的实例传递给类构造函数(请参阅)。据我所知,这有效地防止了为扩展SimpleJpaRepository
的类包含额外的依赖项
我最终以以下方式定义了自定义工厂:
@配置
@ConfigurationProperties(前缀=“spring.jpa.properties.hibernate.jdbc”)
公共类RepositoryConfiguration{
私有整数批量大小;
}
公共类MyCustomRepositoryFactoryBean扩展了JpaRepositoryFactoryBean{
私有存储配置存储配置;
公共MyCustomRepositoryFactoryBean(类)getEntityInformation(information.getDomainType());
返回新的MyCustomRepositoryImpl(
实体信息,
实体管理器,
存储配置);
}
@凌驾
受保护类getRepositoryBaseClass(RepositoryMetadata元数据){
返回MyCustomRepositoryImpl.class;
}
}
}
虽然在MyCustomRepositoryFactoryBean
中也不可能注入带有@Value
的字段,但是Spring解析传递给构造函数的依赖项,因此您可以通过bean提供属性(上面代码中的RepositoryConfiguration
)并将其传递给MyCustomRepositoryImpl
。最后,在通过添加
@EnableJpaRepositories(
RepositoryFactoryBean类=MyCustomRepositoryFactoryBean.class
)
到@配置
注释bean。这是一大堆陈词滥调,但很管用
注意:我使用的是
spring数据jpa:1.11.8。发布版
您可以从EntityManager获得它
String defaultBatchSize = "100";
String batchSizeString = (String) entityManager.getEntityManagerFactory().getProperties()
.getOrDefault("hibernate.jdbc.batch_size", defaultBatchSize);
batchSize = Integer.parseInt(batchSizeString);
如何设置“spring.jpa.properties.hibernate.jdbc.batch_size”值?spring.jpa.properties.hibernate.jdbc.batch_size”是在application.properties中设置的。我的问题是如何将值从那里获取到BulkRepositoryImpl实例中。您得到的错误是什么?BulkRepositoryImpl是在没有错误的情况下创建的。问题在于没有设置batchSize(默认值为0)。是否尝试通过构造函数注入该值?