Spring注入SessionFactory问题

在SSH整合项目中遇到DAO实现类无法通过SessionFactory查询数据的问题,原因为SessionFactory注入失败。通过分析发现,SessionFactory在项目启动时已注入,但Action类调用时需再次注入。解决方案是确保在application.xml中正确配置SessionFactory,并通过set方法在Action中注入StudentDAO,从而间接获取SessionFactory并使用其创建Session,避免空指针异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在做SSH整合的时候,把DAO的实现类和BaseDAO,sessfactory都在application.xml中管理并且注入,但是实际在用到sessfactory查询的时候,总是报错,sessionfactory为空。
application.xml中的部分注入代码

       <!-- 配置sessionFactory相关信息-->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
            </props>  
        </property> 
         <property name="mappingDirectoryLocations">
          <list>
            <value>classpath:com/po</value>
          </list>
        </property>     
        </bean> 

 <!-- 将DAO实现注入到Spring -->
     <bean id="StudentDAO" class="com.dao.StudentDAO"></bean>
     <!-- Action -->
     <bean id="studentQueryAction"
      class="com.action.StudentAction" scope="prototype">
     <property name="StudentDAO" ref="StudentDAO"></property>
     </bean>
      <bean id="BD" class="com.dao.IBaseHibernateDAO">
     <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>

自此已经成功注入了

struts.xml中的Action代码

<!--让spring容器管理struts-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="struts2" extends="struts-default">

<action name="studentquery" class="studentQueryAction">
<result name="success">/views/student.jsp</result>
</action>
</package>
</struts>    

StudentDAO 代码

package com.dao;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.po.Student;
public class StudentDAO extends IBaseHibernateDAO {
    private static final Log log = LogFactory.getLog(CourseDAO.class);
    public static final String NAME = "name";
    public static final String SEX = "sex";

    public void say(){
        System.out.println("DAO已经注入");
    }

    @SuppressWarnings("unchecked")
    //查询所有的学生信息
    public List<Student> findAll() {        
        //去获取SessionFactory产生的session
        ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
        System.out.println("得到Session");
        Query query= sessionFactory.getCurrentSession().createQuery("from Student");
        return query.list();
        }

IBaseHibernateDAO

package com.dao;

import org.hibernate.SessionFactory;

public class IBaseHibernateDAO {

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        System.out.println("SessionFactory已经注入");
    }
}

StudentAction类

package com.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.dao.StudentDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.po.Student;
public class StudentAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private StudentDAO stDAO;
    public void setStudentDAO(StudentDAO stDAO){
        this.stDAO=stDAO;
        System.out.println("StudentDAO已经注入");
    }

    public String execute(){    
        stDAO.say();
        List<Student> list=stDAO.findAll();
    }
} 

问题解决,sessionfactory不再为空,成功获取到了list中的内容,我用System.out.println()获得了整个项目的运行过程。
首先sessionfactory在项目启动时已经被自动注入了。

然后注入的是StudentDAO,其实我们是通过在Action类中,StudentDAO的set方法实现了DAO的注入。

然后去实现DAO中的findAll()方法,从IBaseHibernateDAO中获得了sessfactory,继而得到session。

需要注意的是,在这里sessionFactory注入了2次,项目运行的时候注入,调用的时候注入。

报空指针的原因了,必须从application.xml中去取出sessionfactory;
//去获取SessionFactory产生的session

   ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
        System.out.println("得到Session");

这段代码很重要,去获得sessionfactory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值