Slightly different approach then the above suggested as this is being done from a ServletContextListener and to avoid the use of a factory bean:
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(context); EntityManagerFactory emf = (EntityManagerFactory) wac.getBean("entityManagerFactory"); SessionFactory sf = ((HibernateEntityManagerFactory) emf).getSessionFactory(); ObjectName on = new ObjectName("Hibernate:type=StatisticsService,application=test"); MBeanExporter mbeanExporter = wac.getAutowireCapableBeanFactory().createBean(MBeanExporter.class); mbeanExporter.registerManagedResource(sf.getStatistics(), on);
And for reference:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> ... <property name="persistenceProvider"> <bean class="org.hibernate.jpa.HibernatePersistenceProvider" /> </property> ... </bean>