问题描述
- 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-INFclasseshibernate.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"">
Lblindex.jsp
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilterencoding
UTF-8encodingFilter
*.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
1spring2
/出错的类
@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;}
}
解决方案
@Transactional
@Service-------少了这个吧
public class DaoService implements DaoLbl
解决方案二:
不行呀,DaoService类的jdk事物代理机制不起作用
解决方案三:
谁能帮我调试一下代码
解决方案四:
qq1339665152
解决方案五:
http://blog.csdn.net/evankaka/article/details/45790533