Plant_Ash 2016-06-01 08:45 采纳率: 0%
浏览 1419

Spring注入sessionFactory失败,大神能帮我看下是为什么?

提示空指针java.lang.NullPointerException

这是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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-autowire="byName" default-lazy-init="true">


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass"
            value="com.mysql.jdbc.Driver">
        </property>

         <!-- 服务器数据库  --> 
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/senlin?useUnicode=true&amp;characterEncoding=UTF-8">
        </property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>

        <property name="minPoolSize" value="20"/>
        <property name="maxPoolSize" value="100"/>
        <property name="maxIdleTime" value="10"/>
        <property name="acquireIncrement" value="5"/>
        <property name="maxStatements" value="0"/>
        <property name="initialPoolSize" value="20"/>
        <property name="idleConnectionTestPeriod" value="10"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>cn.edu.zucc.senlin.model.User</value>
            </list>
        </property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property> 
    </bean>

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!-- 哪些类的哪些方法参与事务 -->
    <aop:config proxy-target-class="true">
        <aop:advisor  pointcut="execution(* cn.edu.zucc.senlin.dao.*.*(..))" advice-ref="txAdvice"/>
    </aop:config>

    <bean id="userManage" class="cn.edu.zucc.senlin.action.UserManage" scope="prototype">
        <!-- <property name="userDAO" ref="userDAO"></property> -->
    </bean>

    <bean id="userDAO" class="cn.edu.zucc.senlin.dao.UserDAO">
    </bean>

    <bean id="baseDAO" class="cn.edu.zucc.senlin.dao.BaseDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

</beans>

这是web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="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/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  

    <!-- 加载spring配置文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:*/applicationContext*.xml</param-value>  
    </context-param>  

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- struts2 的配置 -->
    <filter>
            <!-- 定义核心Filter的名字 -->
            <filter-name>struts2</filter-name>
            <!-- 定义核心Filter的实现类 -->
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <!-- FilterDispatcher用来初始化Struts 2并且处理所有的Web请求 -->
    <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>  
    <welcome-file>login.jsp</welcome-file>  
    </welcome-file-list> 
</web-app>

  • 写回答

3条回答 默认 最新

  • qq_21728529 2016-06-01 08:45
    关注

    an并不是从spring容器中拿到。
    正常的应该是:
    UserAction userAction=(UserAction )ApplicationContext.getBean("userAction");

    这样才是属于spring管理,它才能进行依赖注入。
    至于怎么拿到ApplicationContext,你查一下~。

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?