问题描述
我用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注解
解决方案三:
按一楼的方法运行一次吧