5.3.5. 集成器


org.hibernate.integrator.spi.Integrator 旨在提供一种简单的途径,让开发人员能够将信息集中到构建可正常工作的 SessionFactory 的过程中。org.hibernate.integrator.spi.Integrator 接口定义了两种感兴趣的方法:

  • 集成 使我们能够在构建过程中 hook
  • 解除 集成使我们可以固定在 SessionFactory 关机中。
注意

org.hibernate.integrator.spi.Integrator 中定义了第三个方法,它是一个超载的集成形式,接受 org.hibernate.metamodel.source.MetadataImplementor 而不是 org.hibernate.cfg.Configuration

除了 IntegratorService 提供的发现方法外,应用在构建 BootstrapService Registry 时可以手动注册 Integrator 实施。

5.3.5.1. 集成器用例

org.hibernate.integrator.spi.Integrator 注册事件监听程序和提供服务,请参阅 org.hibernate.integrator.spi.ServiceContributingIntegrator

示例:注册事件列表

public class MyIntegrator implements org.hibernate.integrator.spi.Integrator {

    public void integrate(
            Configuration configuration,
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {
        // As you might expect, an EventListenerRegistry is the thing with which event listeners are registered  It is a
        // service so we look it up using the service registry
        final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);

        // If you wish to have custom determination and handling of "duplicate" listeners, you would have to add an
        // implementation of the org.hibernate.event.service.spi.DuplicationStrategy contract like this
        eventListenerRegistry.addDuplicationStrategy(myDuplicationStrategy);

        // EventListenerRegistry defines 3 ways to register listeners:
        //     1) This form overrides any existing registrations with
        eventListenerRegistry.setListeners(EventType.AUTO_FLUSH, myCompleteSetOfListeners);
        //     2) This form adds the specified listener(s) to the beginning of the listener chain
        eventListenerRegistry.prependListeners(EventType.AUTO_FLUSH, myListenersToBeCalledFirst);
        //     3) This form adds the specified listener(s) to the end of the listener chain
        eventListenerRegistry.appendListeners(EventType.AUTO_FLUSH, myListenersToBeCalledLast);
    }
}
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat