从 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);
}
}
但是使用 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);
}
}
如何重新激活我的拦截器?
感谢您的任何提示
问候
里齐
最佳答案
您好 ,
读一读:https://github.com/spring-projects/spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be (使用:HibernatePropertiesCustomizer 接口(interface))
希望这对你有用。
最后一点:总是更新你的 Spring/Hibernate 版本 (尽可能使用最新版本)并且您会看到大多数代码将变得多余,因为较新的版本会尽可能减少配置。
关于hibernate - Spring Boot 2.0.0 M6 - 添加 hibernate 拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47465728/