spring3.1整合hibernate4 注入报空指针异常

问题描述

我用myecplise简单的做了个测试,但是在注入sessionFactory一直不成功,严重:Servlet.service()forservlet[springmvc]incontextwithpath[/springweb]threwexception[Requestprocessingfailed;nestedexceptionisjava.lang.NullPointerException]withrootcausejava.lang.NullPointerExceptionapplicationContext.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd"><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property><propertyname="url"value="jdbc:mysql://localhost:3306/sdkweb"></property><propertyname="username"value="root"></property><propertyname="password"value="1z1z1z"></property></bean><beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><propertyname="dataSource"><refbean="dataSource"/></property><propertyname="hibernateProperties"><props><propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><propkey="hibernate.show_sql">true</prop><propkey="hibernate.current_session_context_class">thread</prop></props></property><propertyname="mappingResources"><list><value>com/hibernate/Webuser.hbm.xml</value></list></property></bean><beanid="WebuserDAO"class="com.hibernate.WebuserDAO"></bean><beanid="Logindao"class="com.Login"><propertyname="sessionFactory"><refbean="sessionFactory"/></property></bean></beans>springmvc-servlet.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"><!--spring注解驱动--><mvc:annotation-driven></mvc:annotation-driven><!--扫描器--><context:component-scanbase-package="com"><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Service"/></context:component-scan><!--配置视图解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/jsp/"></property><propertyname="suffix"value=".jsp"></property></bean></beans>web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>login.javapackagecom;importjavax.annotation.Resource;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importcom.hibernate.Webuser;@ControllerpublicclassLogin{privateSessionFactorysessionFactory;publicvoidsetSessionFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}@RequestMapping("/login.do")publicStringloginstatus(Stringusername,Stringpassword,Modelmodel){System.out.print(username+"----------"+password);Sessionsession=sessionFactory.getCurrentSession();Stringstatus="0";System.out.print(username+"----------"+password);model.addAttribute("status",status);return"index";}}因为是测试所以就简单写了一个Sessionsession=sessionFactory.getCurrentSession();这句运行就报空指针了,应该是注入没成功,不知道是哪错了

解决方案

解决方案二:
setSessionFactory方法上加@Autowired注解
解决方案三:
按一楼的方法运行一次吧

时间: 2024-10-30 19:45:29

spring3.1整合hibernate4 注入报空指针异常的相关文章

junit-strut2+spring3+hibernate4 sessionFactory报错,是怎么回事?

问题描述 strut2+spring3+hibernate4 sessionFactory报错,是怎么回事? 1C ----------------------junit--------------------------------------------------org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of auto

ssh-SSH整合后整合action和业务类总报空指针异常

问题描述 SSH整合后整合action和业务类总报空指针异常 运行异常结果: java.lang.NullPointerException com.web.Controller.sayhello(Controller.java:23) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav

sessionfactory-Spring整合hibernate4时sessionFactory问题

问题描述 Spring整合hibernate4时sessionFactory问题 org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.QueryTimeoutException: Could not open connection org.spring

hibernate进行分页时,sessionFactory为null报空指针异常

问题描述 hibernate进行分页时,sessionFactory为null报空指针异常 从网上查了许都,都没能解决,不知道是配置文件有问题,还是方法写的有问题 解决方案 dao层实现类: /** @ClassName: BaseDaoImpl @Description: 基础dao实现 @author john @date 2015年2月26日 下午3:27:15 */ @SuppressWarnings("unchecked") public abstract class Bas

注释-spring框架,一运行到dao方法就报空指针异常,

问题描述 spring框架,一运行到dao方法就报空指针异常, 查的说是因为dao没有注入,但是我用的是注释,用了@Repository,在配置文件中也用了扫描这个包 解决方案 百度吧,网上一堆相同的问题和解决方案 解决方案二: 你得调试为什么没有注入成功 . 解决方案三: 关键得看那个配置文件· 看名字对应上了没有. 解决方案四: 注入失败,还是要一一检查DAO类上的注解,spring配置,引用DAO的地方. 解决方案五: 已经解决了,是没有加载正确的配置文件 解决方案六: 已经解决了,是没有

spring-求大神!!在Spring3.2和Hibernate4.2.8集成测试的时候出现数据库连不上的错误。。。

问题描述 求大神!!在Spring3.2和Hibernate4.2.8集成测试的时候出现数据库连不上的错误... org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could no

spring-Spring整合hibernate4时出现no session错误

问题描述 Spring整合hibernate4时出现no session错误 首先将eclipse抛出的错误贴出来: 严重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/VideoMngSys] threw exception [Request processing failed; nested exception is org.hibernate.HibernateExceptio

spring-Spring4整合Hibernate4添加事务后出NoSuchMethodError

问题描述 Spring4整合Hibernate4添加事务后出NoSuchMethodError 错误信息: 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error c

loggerfactory-hibernate整合,启动报错!

问题描述 hibernate整合,启动报错! Caused by: java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.search.hcore.impl.HibernateSearchIntegrator could not be instantiated at java.util.ServiceLoader.fail(Unknown Sourc