I was searching the internet for ways to inject Sessions into DAO's this:
HibernateUtil
This form is about implementing a utility class that will configure, instantiate, and make available a org.hibernate.SessionFactory
object that can be used globally (throughout the application).
Base code:
public class HibernateUtil {
private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
public HibernateUtil() {
super();
}
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
return configuration.buildSessionFactory(builder.build());
} catch (Throwable erro) {
System.out
.println("Criação inicial do objeto Sessionfactory falhou. Erro: "
+ erro);
throw new ExceptionInInitializerError(erro);
}
}
Questions
1 - Spring Use, then Spring already gives me the SessionFactory instance according to the configuration I put in the Spring configuration file. And create a class with a property of type SessionFactory that will be injected and a method to make it available seems unnecessary. What is the best way to make Sessions available to DAOs using Spring? Maybe using AspectJ?