There is a class that writes a context DAO layer object to the servlet.

@WebListener public class ContextListener implements ServletContextListener { private final SessionFactory factory = new Configuration() .configure() .buildSessionFactory(); @Override public void contextInitialized(ServletContextEvent sce) { final DAOImp dao = new DAOImp(new AtomicReference<>(factory)); sce.getServletContext().setAttribute("dao", dao); } @Override public void contextDestroyed(ServletContextEvent sce) { factory.close(); } } 

Can the contextInitialized method be contextInitialized before the factory variable gets a value, and why?

Update: SessionFactory is a Hibernate factory.

  • one
    Can not. The variable will be initialized when the object is created. - Mikhail Vaysman
  • one
    And where does the classloader? And it is necessary to specify the framework. - Mikhail Vaysman
  • @Mikhail Vaysman pointed out. - Pavel
  • didn’t know @WebListener related to Hibernate - Mikhail Vaysman

0