/* * Copyright (c) JForum Team. All rights reserved. * * The software in this package is published under the terms of the LGPL * license a copy of which has been included with this distribution in the * license.txt file. * * The JForum Project * http://www.jforum.net */ package net.jforum.core.support.hibernate; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.springframework.context.ApplicationContext; import br.com.caelum.vraptor.ioc.ApplicationScoped; import br.com.caelum.vraptor.ioc.Component; import br.com.caelum.vraptor.ioc.ComponentFactory; /** * @author Rafael Steil */ @Component @ApplicationScoped public class SessionFactoryCreator implements ComponentFactory<SessionFactory> { private SessionFactory factory; private final ApplicationContext applicationContext; public SessionFactoryCreator(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @PostConstruct public void create() { SessionFactory original = new AnnotationConfiguration().configure().buildSessionFactory(); factory = new SpringSessionFactory(applicationContext, original); } @Override public SessionFactory getInstance() { return factory; } @PreDestroy public void destroy() { factory.close(); } }