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 提供的发现方法外,应用在构建 时可以手动注册 Integrator 实施。
BootstrapService Registry
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);
}
}