Riz*_*iCR 5 hibernate interceptor spring-boot
从Spring Boot 2.0.0 M2升级到2.0.0 M6后,我的Hibernate拦截器实现不再起作用。
我以前的实现:
@Configuration
public class HibernateConfiguration extends HibernateJpaAutoConfiguration {
private HibernateStatisticsInterceptor hibernateStatisticsInterceptor;
public HibernateConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers, ObjectProvider<List<SchemaManagementProvider>> providers, HibernateStatisticsInterceptor hibernateStatisticsInterceptor) {
super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers, providers);
this.hibernateStatisticsInterceptor = hibernateStatisticsInterceptor;
}
@Override
protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
vendorProperties.put("hibernate.session_factory.interceptor", hibernateStatisticsInterceptor);
}
}
Run Code Online (Sandbox Code Playgroud)
但是使用M5或M6时,HibernateJpaAutoConfiguration类已更改,并且不再扩展JpaBaseConfiguration。
我尝试为每个YAML配置文件添加拦截器,但是它不起作用。
我的拦截器:
@Component("hibernateStatisticsInterceptor")
public class HibernateStatisticsInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 5278832720227796822L;
private ThreadLocal<Long> queryCount = new ThreadLocal<>();
public void startCounter() {
queryCount.set(0l);
}
public Long getQueryCount() {
return queryCount.get();
}
public void clearCounter() {
queryCount.remove();
}
@Override
public String onPrepareStatement(String sql) {
Long count = queryCount.get();
if (count != null) {
queryCount.set(count + 1);
}
return super.onPrepareStatement(sql);
}
}
Run Code Online (Sandbox Code Playgroud)
如何重新激活拦截器?
谢谢你的提示
问候里兹
你好,
阅读以下内容:https : //github.com/spring-projects/spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be(使用:HibernatePropertiesCustomizer接口)
希望这对您有用。
最后一点:始终更新您的Spring / Hibernate版本(尽可能使用最新版本),随着新版本尝试尽可能减少配置,您将看到大多数代码将变得多余。
我可以用这个文档解决这个问题:
只是实现了一个接口HibernatePropertiesCustomizer
并实现方法customize(Map<String, Object> hibernateProperties)
@Component
class MyHibernateInterceptorCustomizer implements HibernatePropertiesCustomizer {
@Autowired
MyInterceptor myInterceptor
@Override
void customize(Map<String, Object> hibernateProperties) {
hibernateProperties.put("hibernate.session_factory.interceptor", myInterceptor);
}
}Run Code Online (Sandbox Code Playgroud)
由于https://github.com/spring-projects/spring-boot/issues/11211已解决,HibernatePropertiesCustomizer可以从 Spring Boot 2.0.0.RC1 开始使用(在撰写本文时尚未发布,但现在可以使用快照) 。
| 归档时间: |
|
| 查看次数: |
1958 次 |
| 最近记录: |