HibernateSessionFactoryFactory implements DisposableSupplier<SessionFactory> { @Override public void dispose(final SessionFactory sessionFactory) { if (sessionFactory != null && !sessionFactory.isClosed()) { logger.debug("Disposing of hibernate session factory."); sessionFactory.close(); } } @Inject  HibernateSessionFactoryFactory(final ServiceRegistry registry,
                                          final InjectionManager iManager); @Override SessionFactory get(); @Override void dispose(final SessionFactory sessionFactory);  }
@Test public void testProvideDispose() { ServiceRegistry serviceRegistry = injector.getInstance(ServiceRegistry.class); HibernateSessionFactoryFactory factoryFactory = new HibernateSessionFactoryFactory(serviceRegistry, injector); SessionFactory factory = injector.getInstance(SessionFactory.class); assertFalse(factory.isClosed()); Session session = factory.openSession(); assertNotNull(session); assertTrue(session.isOpen()); factoryFactory.dispose(factory); assertTrue(factory.isClosed()); assertFalse(session.isOpen()); factoryFactory.dispose(factory); factoryFactory.dispose(null); }