qq_34173920
大火yzs
采纳率100%
2018-08-27 09:15 阅读 1.2k
已采纳

Spring 注解注入 SessionFactory 空指针异常

5

Spring 注解注入 SessionFactory 空指针异常

条件一
applicationContext.xml文件已经配置了SessionFactory

 <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 引用上面的dataSource配置数据库 -->
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <!-- Hibernate的一些属性配置 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- Hibernate的配置方言 -->
                <prop key="hibernate.show_sql">true</prop><!-- Hibernate生成的sql语句是否显示 -->
            </props>
        </property>

        <property name="mappingResources">
            <list>
                <value>com/bigfire/entity/Sale.hbm.xml</value>
            </list>
        </property>

条件二 SessionFactory 已经成功注入到Spring容器中,

ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
String[] str=act.getBeanDefinitionNames();
for (String string : str) {
System.out.println("..."+string);
}
图片说明

这个问题我百度了两天了,有些方法也都试过了,哪位同学解决了麻烦给分享下解决的办法,谢谢了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

9条回答 默认 最新

  • 已采纳
    qq_34173920 大火yzs 2018-08-27 11:24

    谢谢各位,已经解决了,自己Sevlet和Biz层有new出来的类,没有让Spring 管理,所以导致SessionFactory没法注入。

    点赞 评论 复制链接分享
  • tongkaiming tongkaiming 2018-08-27 09:58

    hibernateTemplate已经注入了sessionfactory,不需要再次注入sessionfactory

    点赞 1 评论 复制链接分享
  • niaonao niaonao 2018-08-31 06:06

    既然结贴了,就别挂着了,
    答题的人没必要再点进来,
    需要解决该问题的人看到没结贴也不会进来,结贴了有助于需要帮助的人来借鉴问题的处理经验。

    点赞 1 评论 复制链接分享
  • zhaomin_g _zming 2018-08-27 09:23

    代码日志发出来看看,怎么个空指针

    点赞 评论 复制链接分享
  • tongkaiming tongkaiming 2018-08-27 09:28

    applicationContext.xml发全点

    点赞 评论 复制链接分享
  • qq_34173920 大火yzs 2018-08-27 09:29

    Tomcat成功启动,

     八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Server version:        Apache Tomcat/9.0.10
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Server built:          Jun 20 2018 17:32:21 UTC
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Server number:         9.0.10.0
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: OS Name:               Windows 7
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: OS Version:            6.1
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Architecture:          amd64
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Java Home:             E:\zheng\java\jre1.8
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: JVM Version:           1.8.0_181-b13
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: JVM Vendor:            Oracle Corporation
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: CATALINA_BASE:         E:\zheng\software\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: CATALINA_HOME:         E:\zheng\software\Tomcat\apache-tomcat-9.0.10
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dcatalina.base=E:\zheng\software\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dcatalina.home=E:\zheng\software\Tomcat\apache-tomcat-9.0.10
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dwtp.deploy=E:\zheng\software\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Djava.endorsed.dirs=E:\zheng\software\Tomcat\apache-tomcat-9.0.10\endorsed
    八月 27, 2018 5:28:32 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dfile.encoding=UTF-8
    八月 27, 2018 5:28:32 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [E:\zheng\java\jre1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;E:/zheng/java/jre1.8/bin/server;E:/zheng/java/jre1.8/bin;E:/zheng/java/jre1.8/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;.;E:\zheng\java\jdk1.8.0_181\bin;E:\zheng\java\jre1.8\bin;C:\Program Files\dotnet\;C:\Program Files\MySQL\MySQL Server 5.7\bin;E:\zheng\js\node\nodejs\;E:\zheng\software\vscode\Microsoft VS Code\bin;E:\zheng\github\git\Git\cmd;C:\Users\win7\AppData\Roaming\npm;E:\zheng\software\eclipse\eclipse;;.]
    八月 27, 2018 5:28:33 下午 org.apache.coyote.AbstractProtocol init
    信息: Initializing ProtocolHandler ["http-nio-8080"]
    八月 27, 2018 5:28:33 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    信息: Using a shared selector for servlet write/read
    八月 27, 2018 5:28:33 下午 org.apache.coyote.AbstractProtocol init
    信息: Initializing ProtocolHandler ["ajp-nio-8009"]
    八月 27, 2018 5:28:33 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    信息: Using a shared selector for servlet write/read
    八月 27, 2018 5:28:33 下午 org.apache.catalina.startup.Catalina load
    信息: Initialization processed in 749 ms
    八月 27, 2018 5:28:33 下午 org.apache.catalina.core.StandardService startInternal
    信息: Starting service [Catalina]
    八月 27, 2018 5:28:33 下午 org.apache.catalina.core.StandardEngine startInternal
    信息: Starting Servlet Engine: Apache Tomcat/9.0.10
    八月 27, 2018 5:28:36 下午 org.apache.jasper.servlet.TldScanner scanJars
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    八月 27, 2018 5:28:36 下午 org.apache.catalina.startup.HostConfig deployDescriptor
    信息: Deploying deployment descriptor [E:\zheng\software\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\WaterWarm3.0.xml]
    八月 27, 2018 5:28:36 下午 org.apache.catalina.startup.HostConfig deployDescriptor
    警告: The path attribute with value [/WaterWarm3.0] in deployment descriptor [E:\zheng\software\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\WaterWarm3.0.xml] has been ignored
    八月 27, 2018 5:28:36 下午 org.apache.catalina.startup.SetContextPropertiesRule begin
    警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:WaterWarm3.0' did not find a matching property.
    八月 27, 2018 5:28:42 下午 org.apache.jasper.servlet.TldScanner scanJars
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    八月 27, 2018 5:28:42 下午 org.apache.catalina.core.ApplicationContext log
    信息: Initializing Spring root WebApplicationContext
    八月 27, 2018 5:28:46 下午 org.apache.catalina.startup.HostConfig deployDescriptor
    信息: Deployment of deployment descriptor [E:\zheng\software\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\WaterWarm3.0.xml] has finished in [10,474] ms
    八月 27, 2018 5:28:46 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["http-nio-8080"]
    八月 27, 2018 5:28:46 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["ajp-nio-8009"]
    八月 27, 2018 5:28:46 下午 org.apache.catalina.startup.Catalina start
    信息: Server startup in 13841 ms
    

    Tomcat启动完成,请求自己的接口

    请求接口时,报空指针异常, 空指针异常对象是SessionFactory
    说明这个对象没有注入进去,

     null
    八月 27, 2018 5:31:08 下午 org.apache.catalina.core.StandardWrapperValve invoke
    严重: Servlet.service() for servlet [SaleLogin] in context with path [/WaterWarm3.0] threw exception
    java.lang.NullPointerException
        at com.bigfire.daoimpl.SaleDaoImpl.findByNameAndPwd(SaleDaoImpl.java:61)
        at com.bigfire.bizimpl.SaleBizImpl.login(SaleBizImpl.java:21)
        at com.bigfire.sevlet.SaleLogin.doGet(SaleLogin.java:22)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at com.bigfire.filter.CORSFilter.doFilter(CORSFilter.java:21)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
    
    点赞 评论 复制链接分享
  • u011924665 大尾巴 2018-08-27 09:29

    你虽然将SessionFactory对象注入进去了。但是你是用SessionFactory对象的这个类有没有注入进去?
    即你在A类中使用B对象,你将B对象注入到了Spring容器,但是没有将A注入进去。Spring自然不会将B注入到A对象中,只是将B放在了容器中。

    点赞 评论 复制链接分享
  • tongkaiming tongkaiming 2018-08-27 09:31

    SaleDaoImpl类61行,什么代码

    点赞 评论 复制链接分享
  • qq_34173920 大火yzs 2018-08-27 09:42

    applicationContext.xml 文件

     <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/task
         http://www.springframework.org/schema/task/spring-task-3.0.xsd 
         ">
        <!--spring扫描com.bigfire包下带有component注解的类 -->
    
        <context:component-scan base-package="com.bigfire"></context:component-scan>
        <context:annotation-config/>
    
        <!--spring开启事务注解 功能 
        <tx:annotation-driven transaction-manager="transactionManager"/>
        -->
        <tx:annotation-driven /> 
        <!--spring开启切面编程 功能 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        <!--spring 数据库连接池 功能 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!--数据库设备名称 -->
            <property name="url" value="jdbc:mysql:///bbase"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
    
        </bean>
        <!-- Hibernate的SessionFactory 对象的创建交给 Spring 进行管理 -->
        <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <!-- 引用上面的dataSource配置数据库 -->
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
            <!-- Hibernate的一些属性配置 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- Hibernate的配置方言 -->
                    <prop key="hibernate.show_sql">true</prop><!-- Hibernate生成的sql语句是否显示 -->
                </props>
            </property>
    
            <property name="mappingResources">
                <list>
                    <!-- 
                    <value>com/bigfire/entity/Address.hbm.xml</value>
                    <value>com/bigfire/entity/Goods.hbm.xml</value>
                    <value>com/bigfire/entity/Goodsclass.hbm.xml</value>
                    <value>com/bigfire/entity/Goodsgroup.hbm.xml</value>
                    <value>com/bigfire/entity/Mapping.hbm.xml</value>
                    <value>com/bigfire/entity/Order.hbm.xml</value>
                    <value>com/bigfire/entity/Orderdetails.hbm.xml</value>
                     -->
                    <value>com/bigfire/entity/Sale.hbm.xml</value>
                </list>
            </property>
    
        </bean>
        <!--
        <bean id="SaleDaoImpl" class="com.bigfire.daoimpl.SaleDaoImpl">
            <property name="sessionFactory"> <ref bean="sessionFactory" /> </property>
        </bean>
        -->
        <!--hibernateTemplate的配置 -->
        <bean id="hibernateTemplate"
            class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!--事务的配置 -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    </beans>
    
    点赞 评论 复制链接分享

相关推荐