hibernate报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory me.gacl.dao.impl.UserDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring-hibernate.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring-hibernate.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'maxIdle' threw exception; nested exception is java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)09:40:4009:40:4309:40:4309:40:44at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at me.gacl.test.TestHibernate.before(TestHibernate.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)

 这个报错出现在userDao里面,sessionfactory没有注入。

解决办法:

 

sessionfactory不能注入有如下几个原因:
1.没写@Repository("userDao")
2.<!-- Spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param>
少了classpath:spring-hibernate.xml
3.仔细检查<!-- 配置hibernate session工厂 -->

spring整合Hibernate使用test测试时要把spring.xml和spring-hibernate.xml这两个配置文件加载进来

/通过spring.xml配置文件创建Spring的应用程序上下文环境
        //ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
        /**
         *因为已经整合了Hibernate,UserServiceImpl类中使用到了userDao,
         *userDao是由spring创建并且注入给UserServiceImpl类的,而userDao中又使用到了sessionFactory对象
         *而创建sessionFactory对象时需要使用到spring-hibernate.xml这个配置文件中的配置项信息,
         *所以创建Spring的应用程序上下文环境时,需要同时使用spring.xml和spring-hibernate.xml这两个配置文件
         *否则在执行Maven install命令时,因为maven会先执行test方法中的代码,而代码执行到
         *UserServiceI userService = (UserServiceI) ac.getBean("userService");
         *这一行时就会因为userDao中使用到sessionFactory对象无法正常创建的而出错,这样执行Maven install命令编译项目时就会失败!
         *
         */
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});

  

posted @ 2017-03-30 09:46  crud程序员  阅读(701)  评论(0编辑  收藏  举报