Explain, please, I am quite new to everything that goes beyond the java core. Problem following at start webapp on tomcat exception takes off. How to fix, I do not understand.

source code posted on dropbox

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class ru.haki.utils.HibernateSessionFactory org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1305) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:979) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) root cause java.lang.NoClassDefFoundError: Could not initialize class ru.haki.utils.HibernateSessionFactory ru.haki.service.UserService.getAllUsers(UserService.java:27) ru.haki.controller.MainController.main(MainController.java:24) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

The Could not initialize class ru.haki.utils.HibernateSessionFactory occurs due to a masked crash when initializing the static sessionFactory field in this code.

 try { session = HibernateSessionFactory.getSession(); session.getTransaction().begin(); users = session.createQuery("FROM User").list(); } catch(HibernateException e){ e.printStackTrace(); }finally{ HibernateSessionFactory.closeSession(session); } 

When you first access HibernateSessionFactory when you call session = HibernateSessionFactory.getSession(); initialization error occurs, but since in the finally block there is a repeated call to HibernateSessionFactory which has already been tried unsuccessfully to initialize, then the original error is masked, and the exception NoClassDefFoundError . If you comment out the call to HibernateSessionFactory.closeSession(session); then you can see the original exception

 java.util.ServiceConfigurationError: org.hibernate.boot.registry.selector.StrategyRegistrationProvider: Provider org.hibernate.cache.infinispan.StrategyRegistrationProviderImpl not found java.util.ServiceLoader.fail(Unknown Source) java.util.ServiceLoader.access$300(Unknown Source) java.util.ServiceLoader$LazyIterator.next(Unknown Source) java.util.ServiceLoader$1.next(Unknown Source) org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:340) org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.buildSelector(StrategySelectorBuilder.java:162) org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:222) org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:58) ru.haki.utils.HibernateSessionFactory.buildSessionFactory(HibernateSessionFactory.java:19) 

on which, unfortunately, I can not tell.

For the future.
Try to avoid self-made Singleton objects (if you still need, read this article ) and use the opportunities provided by the frameworks used, in your case Spring. And I advise you to put the HibernateSessionFactory initialization code into the static initialization side, something like this:

 public class HibernateSessionFactory { private static SessionFactory sessionFactory = null; { try { sessionFactory = buildSessionFactory(); } catch (Exception ex) { //TODO: добавить логирование throw ex; } } } 

Although more correct, in my opinion, from this to make Spring Bean.

  • Do not tell me the correct article on Spring Bean? - Shakirov Ramil
  • The official documentation is here . A good lesson in Russian is here - Ilya
  • Through bins made. The problem is similar - Shakirov Ramil