hibernate4.3.5中使用spring4.0.0注入sessionFactory出现null

最近在搞SSH整合,遇到问题了,就是spring注入sessionFactory失败
报错如下
type Exception report
message
description The server encountered an internal error that prevented
it from fulfilling this request.
exception
java.lang.NullPointerException
com.victor.dao.impl.BaseDaoImpl.getSession(BaseDaoImpl.java:46)
com.victor.dao.impl.BaseDaoImpl.search(BaseDaoImpl.java:56)
com.victor.dao.impl.UserDaoImpl.isValidte(UserDaoImpl.java:16)
com.victor.actions.LoginAction.execute(LoginAction.java:23)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)

5个回答

hibernate.cfg.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


org.gjt.mm.mysql.Driver
Hurk

<![CDATA[jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=utf8]]>

root
org.hibernate.dialect.MySQLInnoDBDialect
true
true
update



实现类如下
package com.victor.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.victor.dao.IBaseDao;
import com.victor.entity.User;

public class BaseDaoImpl implements IBaseDao
{
private SessionFactory sessionFactory;
private Session session;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Override
public Session getSession()
{
return sessionFactory.getCurrentSession();
}
@Override
public List search(String hql)
{
User user = new User();
getSession().save(user);

    List list = new ArrayList();
    list = sessionFactory.getCurrentSession().createQuery(hql).list();

    session.close();
    return list;
}
@Override
public List searchBook(String hql)
{

    List list = null;
    list = sessionFactory.getCurrentSession().createQuery(hql).list();

    session.close();
    return list;
}

}

applicationContext.xml如下

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">
<!-- 配置spring注解 -->
context:annotation-config/
<!-- -->
<!-- datasource -->






<!-- spring与hibernate整合 spring来管理session的创建、打开和关闭 -->

<!-- 通过配置文件的方式获取数据源,出现异常,未解决 -->

     <property name="hibernateProperties">
         <props>
             <prop key="connection.useUnicode">true</prop>
             <prop key="connection.characterEncoding">utf-8</prop>
             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
             <prop key="hibernate.show_sql">true</prop>
             <prop key="hibernate.hbm2ddl.auto">update</prop>
              <prop key ="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>  
         </props>
     </property>
     <property name="annotatedClasses">
         <list>
             <!-- 以下用来列出所有的PO映射文件 -->
             <value>com.victor.entity.User</value>
             <value>com.victor.entity.Book</value>
         </list>
     </property>
 </bean>
   <!-- 配置事务管理器 -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务的传播特性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="search*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="modify*" propagation="REQUIRED" />
         <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
        <tx:method name="*" propagation="REQUIRED" read-only="true" />
    </tx:attributes>
</tx:advice>

<!-- 那些类那些方法使用事务 -->
<aop:config>
  <!-- 只对业务逻辑层实施事务 -->
    <aop:pointcut id="allManagerMethod"
        expression="execution(* com.victor.dao.impl.*.*(..))" />
    <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />
</aop:config>
<!-- Spring管理Struts2的Action -->
 <bean name="loginAction" class="com.victor.actions.LoginAction" scope="prototype"/> 
 <bean name="searchAction" class="com.victor.actions.SearchAction" scope="prototype"/>  
 <!-- spring管理struts2的intercepter -->
 <bean id="checkLoginInterceptor" class="com.victor.inter.CheckLogin" scope="prototype"/>
 <bean id="baseDaoImpl" class="com.victor.dao.impl.BaseDaoImpl">
   <property name="sessionFactory" ref="sessionFactory"/>
 </bean>


hibernate.cfg.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


org.gjt.mm.mysql.Driver
Hurk

<![CDATA[jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=utf8]]>

root
org.hibernate.dialect.MySQLInnoDBDialect
true
true
update



 实现类如下
```package com.victor.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.victor.dao.IBaseDao;
import com.victor.entity.User;

public class BaseDaoImpl implements IBaseDao
{
    private SessionFactory sessionFactory;
    private Session session;
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    @Override
    public Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }
    @Override
    public List search(String hql)
    {
        User user = new User();
        getSession().save(user);
        List list = new ArrayList();
        list = sessionFactory.getCurrentSession().createQuery(hql).list();
        session.close();
        return list;
    }
    @Override
    public List searchBook(String hql)
    {

        List list = null;
        list = sessionFactory.getCurrentSession().createQuery(hql).list();
        session.close();
        return list;
    }
}







你直接把项目打包到网上不是更方便?如果是你个人的项目的话

项目只是半成品,而且打包也是麻烦。如果还需要什么文件我可以发上来,感觉是配置问题,但是有找不出来哪里配置错了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!