On the advice of odnoforuchans, I try to describe the bins with annotations and configure them with a class. When deployed in a browser in trace, it displays the following

Stack Trace org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ua.service.impl.ClientServiceImpl ua.controller.MainController.clientService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ua.dao.impl.ClientDaoImpl ua.service.impl.ClientServiceImpl.clientDaoImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory ua.dao.impl.ClientDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with session name defined in class path [ua / config / WebConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.hibernate.SessionFactory ua.config.WebConfig.getSessionFactory ()] threw exception; nested ohm populateBean (AbstractAutowireCapableBeanFactory.java:1186) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:537) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:475) org. springframework.beans.factory. .doGetBean (AbstractBeanFactory.java:298) org.springframework. beans.factory.support.abb.com java manger createWebApplication.com (FrameworkService) website servlet.FrameworkServlet.initServletBean (FrameworkServlet.java:484) org.springframework.web.servle t.HttpServletBean.init (HttpServletBean.java:136) javax.servlet.GenericServlet.init (GenericServlet.java:244) io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed (LifecyleInterceptorInvocation.java:117) org.wildfly.extension. undertow.security.RunAsLifecycleInterceptor.init (RunAsLifecycleInterceptor.java:79) io. 220) io.undertow.servlet.core.ManagedServlet.getServlet (ManagedServlet.java:163) io.undertow.servlet.handlers.ServletHandler.handleRequest (ServletHandler.java:85) io.undertow.servlet.handlers.e.e.e.e.e. handleRequest (ServletSecurityRoleHandler.java:62) io. iationHandler.java:78) io.undertow.server. handlers.security.ServletAuthenticationCallHandler.handleRequest (ServletAuthenticationCallHandler.java:57) io.undertow.server.handlers.PredicateHandler.handleRequest (PredicateHandler.java:43) io.undertow.security.handlers.raps.rabs.aud.rabs.aud.degrin.http.http.http.htmlvandAuthenticationCallHandler.handleRequest (ServletAuthenticationCallHandler.java:57) io.underto.security.http.http io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest (svc) modern technology ipodit.de.ukh.de.tr handleRequest (CachedAuthenticatedSessionHandler.java:70) io.undertow.security.handlers.SecurityInitialHandl er.handleRequest (SecurityInitialHandler.java:76) io.undertow.server.handlers.PredicateHandler. io.undertow.server.handlers.PredicateHandler. java: 261) io.undertow.servlet. $ 1.handleRequest (ServletInitialHandler.java:167) io.undertow.server. Connect. .runWorker ( ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617) java.lang.Thread.run (Thread.java:745)

There are classes:

Webconfig

@Configuration @EnableWebMvc @ComponentScan ("ua") public class WebConfig { Logger logger = Logger.getLogger(WebConfig.class); @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/pages"); resolver.setSuffix(".jsp"); return resolver; } @Bean (name = "sessionFactory") public SessionFactory getSessionFactory() { org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration(); SessionFactory sessionFactory = configuration.configure("resources/hibernate.cfg.xml").buildSessionFactory(); logger.info("get Session Factory"); return sessionFactory; } @Bean public ClientDaoImpl dao(){ return new ClientDaoImpl(); } @Bean public ClientServiceImpl service(){ return new ClientServiceImpl(); } } 

DAO interface and its implementation

 public interface ClientDao { public void addClient(Client client); public List<Client> listClient(); public void removeClient (Client client); } @Repository public class ClientDaoImpl implements ClientDao { @Autowired @Qualifier(value = "sessionFactory") private SessionFactory sessionFactory; @Override public void addClient(Client client) { sessionFactory.getCurrentSession().save(client); sessionFactory.close(); } @Override public List<Client> listClient() { List<Client> result = sessionFactory.getCurrentSession().createCriteria(Client.class).list(); sessionFactory.close(); return result; } @Override public void removeClient(Client client) { sessionFactory.getCurrentSession().delete(client); sessionFactory.close(); } } 

Service interface and its implementation

 public interface ClientService { public void addClient(Client client); public List<Client> listClient(); public void removeClient(Client client); } @Service public class ClientServiceImpl implements ClientService { @Autowired private ClientDaoImpl clientDaoImpl; @Override @Transactional public void addClient(Client client) { clientDaoImpl.addClient(client); } @Override @Transactional public List<Client> listClient() { return clientDaoImpl.listClient(); } @Override @Transactional public void removeClient(Client client) { clientDaoImpl.removeClient(client); } } 

Controller

 @Controller public class MainController { @Qualifier("service") @Autowired private ClientServiceImpl clientService; @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView mainPage (){ ModelAndView modelAndView = new ModelAndView(); List<Client> allClient = clientService.listClient(); modelAndView.addObject("allClient", allClient); modelAndView.setViewName("index"); return modelAndView; } } 

web.xml

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Blog Servlet</display-name> <servlet> <servlet-name>servletController</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>ua.config</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>servletController</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 

hibernate.cfg.xml

 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/postschema</property> <property name="connection.username">root</property> <property name="connection.password">peroser12</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="ua.model.Client"/> </session-factory> </hibernate-configuration> 

As I understand there is an error in the description of bins and sessionFactory. But exactly the mechanism on which there are exceptions, I can not find. Thanks in advance for your help.

  • 2
    In the framerass it is explicitly written where the error resources/hibernate.cfg.xml not found . If you have maven, the contents of the src/main/resources directory are copied to the classpath, not the directory itself. Those. you only need to specify the file name. - enzo
  • And please, next time draw out the patterns / logs as a code, and not as a quote. - Roman
  • @enzo partially solved the problem. Currently in trace the following. org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Program Files\Java\apache-tomcat-8.0.32\webapps\ROOT\WEB-INF\classes\ua\config\WebConfig.class]; nested exception is java.lang.IncompatibleClassChangeError: org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:260) org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Program Files\Java\apache-tomcat-8.0.32\webapps\ROOT\WEB-INF\classes\ua\config\WebConfig.class]; nested exception is java.lang.IncompatibleClassChangeError: org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:260)
  • @Roman Good. thank. - Dmitriy Smirnov
  • @ Dmitriy is another problem - probably we’ve been addicted to maven with dependencies. It is impossible in one thread to consistently disassemble all the mistakes that you have while you learn. There may be many of them, and all will require different input. You need to either change the approach - ask questions "How to make X?", Or learn to read and analyze the patterns, or arrange one question for each case. And better all at once :) - enzo

1 answer 1

You must have something like DAO

 private static SessionFactory sesFactory = new Configuration().configure().buildSessionFactory(); 

This, in fact, will create a SessionFactory, and you are trying to implement something that is not yet in the context of Spring. But this method is far from the best.

And here is the configuration for Spring:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="ua.dp.daragan.spring000.domain" /> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> </bean> 

After that, you can set up Autowired in DAO:

  private SessionFactory sessionFactory; @Autowired public HibernateSpitterDao(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }