1
I was researching on the internet ways to inject Sessions in the DAO’s this:
Hibernateutil
This way is to implement a utility class that will configure, instantiate and make available an object org.hibernate.SessionFactory
which 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 provides me the instance of Sessionfactory according to the configuration I put in the Spring configuration file. And creating a class with a Sessionfactory-like property that will be injected and a method to make it available seems unnecessary. What is the best way to make Sessions available to DAO’s using Spring? Maybe, use Aspectj?