xinming_10 2015-07-21 06:47 采纳率: 0%
浏览 1547

springmvc sessionFactory is null

严重: Servlet.service() for servlet [spring2] in context with path [/Lbl] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException

说明:@resource和@annotional都不起作用。
网上的方法都试过了,不起作用
spring2-servlet.xml

<?xml version="1.0" encoding="UTF-8" ?>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.lbl.active" />
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

<bean id="bookXmlView" class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg>
        <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
            <property name="autodetectAnnotations" value="true"/>
        </bean>
    </constructor-arg>
</bean>

<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>

application.xml

context:annotation-config/
<!-- 自动扫描与装配 -->
/context:component-scan

<!-- 导入外部的properties--> 
<context:property-placeholder location="classpath:jdbc.properties"/> 


<!--配置session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 配置数据库路径 classpath:WEB-INF\classes\hibernate.cfg.xml-->
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
     <!-- 配置数据库c3p0连接池  dataSource写错导致bean创建异常错误信息属性值设置错误 -->
    <property name="dataSource" >  <!-- dataSouce应该写成dataSource第三个错误 -->

    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
     <!-- 配置数据库信息 -->
   <property name="jdbcUrl" value="${jdbcUrl}"></property>
   <property name="driverClass" value="${driverClass}"></property>
   <property name="user" value="${user}"></property>
   <property name="password" value="${password}"></property> 
   <!-- 其他配置 -->    
</bean>
</property>
</bean>  

<!-- 配置声明事物管理  采用注解方式-->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
    <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
   <tx:annotation-driven transaction-manager="txManager"/>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
Lbl

index.jsp


contextConfigLocation

/WEB-INF/applicationContext.xml




org.springframework.web.context.ContextLoaderListener


encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



encodingFilter
*.do

   <filter> 
  <filter-name>openSession</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 <init-param>
     <param-name>sessionFactoryBeanName</param-name>
     <param-value>sessionFactory</param-value>
 </init-param>
  </filter>

  <filter-mapping>
  <filter-name>openSession</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>


spring2
org.springframework.web.servlet.DispatcherServlet
1


spring2
/

出错的类

@Transactional
public class DaoService implements DaoLbl{

@Resource
private SessionFactory sessionFactory;
private Class clazz;

@SuppressWarnings("unchecked")
public DaoService(){
System.out.println("---1---->>>:"+this.getClass());
// System.out.println("----2--->>>:"+this.getClass().getGenericSuperclass());
//使用反射技术获得 T的真实类型
ParameterizedType pt=(ParameterizedType)this.getClass()//
.getGenericSuperclass();//获取当前new的泛型父类类型
System.out.println("------->:");
this.clazz=(Class)pt.getActualTypeArguments()[0];
//获取第一个参数的实际类型
System.out.println("class--->" + clazz);
}

 protected Session getSession(){

//

// String path="C:/Users/admin/workspace/Lbl/WebContent/WEB-INF/applicationContext.xml";
// ApplicationContext ac=new FileSystemXmlApplicationContext(path);
// SessionFactory sessionFactory=(SessionFactory)ac.getBean("sessionFactory");
// System.out.println("00001113333333----000->"+ sessionFactory);
// System.out.println("00001113333333----000->"+ sessionFactory.openSession());

// return sessionFactory.openSession();
// System.out.println("0000000->"+ sessionFactory.getCurrentSession());
try {
return sessionFactory.getCurrentSession();
} catch (Exception e) {
e.getStackTrace();
}
return null;

    }

}

  • 写回答

5条回答 默认 最新

  • 丵鹰 2015-07-21 06:54
    关注

    @Transactional
    @Service-------少了这个吧
    public class DaoService implements DaoLbl

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题