Spring Boot 2.0.0 M6-添加休眠拦截器 [英] Spring Boot 2.0.0 M6 - Add Hibernate Interceptor
问题描述
从Spring Boot 2.0.0 M2升级到2.0.0 M6后,我的Hibernate拦截器实现不再起作用.
After upgrade from Spring Boot 2.0.0 M2 to 2.0.0 M6 my Hibernate interceptor implementation don't work anymore.
我以前的实现方式:
@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);
}
}
但是对于M5或M6,HibernateJpaAutoConfiguration类已更改,并且不再扩展JpaBaseConfiguration.
But with M5 or M6 the HibernateJpaAutoConfiguration class changed and extends JpaBaseConfiguration no more.
我尝试为每个YAML配置文件添加拦截器,但是它不起作用.
I try to add my interceptor per YAML-Configuration file, but it's not working.
我的拦截器:
@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);
}
}
如何重新激活拦截器?
感谢任何提示
致谢 里兹
推荐答案
你好,
请阅读以下内容: https://github.com/spring-projects /spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be (使用:HibernatePropertiesCustomizer接口)
Give this a read: https://github.com/spring-projects/spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be (use: HibernatePropertiesCustomizer interface)
- 实施.
- @Override custom()方法,添加拦截器
- 别忘了 @Lazy注入(如果使用内部bean)
- 完成=>运行
- Implement it.
- @Override customize() method, add your interceptor
- Don't forget to @Lazy inject in case of internal beans
- Done => Run
希望这对您有用.
最后一点:总是更新您的Spring/Hibernate版本(尽可能使用最新版本),随着新版本尝试尽可能减少配置,您将看到大多数代码将变得多余.可能.
As a final note: always update your Spring / Hibernate versions (use the latest as possible) and you will see that most code will become redundant as newer versions try to reduce the configurations as much as possible.
这篇关于Spring Boot 2.0.0 M6-添加休眠拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!