【问题标题】:Spring Boot 2.0.0 M6 - Add Hibernate InterceptorSpring Boot 2.0.0 M6 - 添加休眠拦截器
【发布时间】:2017-11-24 02:57:56
【问题描述】:

从 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);
    }

}

如何重新激活我的拦截器?

感谢任何提示

问候 里齐

【问题讨论】:

  • 您可以尝试从github.com/spring-projects/spring-boot/blob/master/… 扩展而不是 HibernateJpaAutoConfiguration 吗?
  • 这不是一个公共类,不能在它的包之外使用:/
  • 经过更多研究后,我找到了添加 Hibernate 拦截器的方法。你可以通过properties/yml文件添加拦截器,但是我先试错了地方。应该是 spring.jpa.properties.hibernate.session_factory.interceptor = &lt;class with path&gt; 我的新问题是,这个休眠创建了他自己的这个类的实例,并且与我从 spring 的实例不同:/关于 Rizzi

标签: hibernate spring-boot interceptor


【解决方案1】:

你好,

阅读:https://github.com/spring-projects/spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be(使用:HibernatePropertiesCustomizer 接口)

  1. 实现它。
  2. @Override customize() 方法,添加你的拦截器
  3. 不要忘记 @Lazy 注入,以防内部 bean
  4. 完成 => 运行

希望这对你有用。

最后一点:始终更新您的 Spring / Hibernate 版本(尽可能使用最新版本),您会发现大多数代码将变得多余,因为新版本会尽可能地减少配置可能。

【讨论】:

  • @Lazy 是在将repositories 自动连接到拦截器时完成它的技巧。谢谢,您节省了我的时间。
【解决方案2】:

我可以用这个文档解决这个问题:

Add support for advanced customization of Hibernate settings

只实现一个接口HibernatePropertiesCustomizer

并实现方法customize(Map hibernateProperties)

@Component
class MyHibernateInterceptorCustomizer implements HibernatePropertiesCustomizer {

    @Autowired
    MyInterceptor myInterceptor

    @Override
    void customize(Map<String, Object> hibernateProperties) {
       hibernateProperties.put("hibernate.session_factory.interceptor", myInterceptor);
    }
}

【讨论】:

    【解决方案3】:

    https://github.com/spring-projects/spring-boot/issues/11211 已解决,HibernatePropertiesCustomizer 可以从 Spring Boot 2.0.0.RC1 开始使用(在撰写本文时尚未发布,但现在可以使用快照)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2020-01-21
      • 2018-08-09
      • 2019-06-18
      • 1970-01-01
      相关资源
      最近更新 更多