Spring4 Hibernate3 整合SessionFactory抛出空异常
昨天在做Spring 和 Hibernate的整合时出现的问题,一直没能解决,求助:
说明:我创建的是一个web工程,然后在测试的时候使用的是Junit
看源码:
测试类:
public class Test {
//创建Spring 工厂
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
@org.junit.Test
public void add(){
IUserService userService = factory.getBean("userService", UserService.class);
User user = new User();
user.setName("Defens");
userService.add(user);
}
}
业务处理类:
public class UserService implements IUserService{
private IUserDao userDao;
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
@Override
public void add(User user) {
System.out.println("添加用户服务层............");
IUserDao userDao = new UserDaoImpl();
userDao.save(user);
}
}
持久化操作类:
public class UserDaoImpl extends BaseDao implements IUserDao {
@Override
public void save(User user) {
System.out.println("添加用户持久化层............");
//从父对象获取
Session session = getSession();
session.save(user);
}
}
持久化父类
public class BaseDao {
private SessionFactory factory;
public SessionFactory getFactory() {
System.out.println("获取SessionFactory.............");
if(this.factory == null){
System.out.println("SessionFactory为空.............");
}else {
System.out.println("SessionFactory不为空:" + this.factory.getClass().getName());
}
return this.factory;
}
public void setFactory(SessionFactory factory) {
System.out.println("注入SessionFactory.............");
if(factory == null){
System.out.println("注入的SessionFactory为空.............");
}else{
System.out.println("注入的SessionFactory不为空.............");
System.out.println("注入的SessionFactory:" + factory.getClass().getName());
}
this.factory = factory;
System.out.println("######################################");
getFactory().openSession();
System.out.println("######################################");
}
public Session getSession(){
return getFactory().openSession();
}
public void destory() {
System.out.println("销毁BaseDao的Bean.................");
}
public void init(){
System.out.println("初始化BaseDao的Bean.................");
}
}
beans.xml的配置文件
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--
此处指明相关的hibernate资源文件
-->
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<!--配置Hibernate相关的属性-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<bean id="baseDao" class="cn.yesol.defens.dao.impl.BaseDao" scope="singleton">
<property name="factory" ref="sessionFactory"/>
</bean>
<bean id="userDao" class="cn.yesol.defens.dao.impl.UserDaoImpl" scope="singleton">
<!--<property name="sessionFactory" ref="sessionFactory" />-->
</bean>
<bean id="userService" class="cn.yesol.defens.service.impl.UserService" scope="prototype">
<property name="userDao" ref="userDao" />
</bean>
我整个是没有使用Annotation的,全部是基于XML的配置,在测试类运行后,查看数据库,相关的表是生成了的,我在注入的set方法中加了一些输出,从输出来看,确实是注入成功了的,同时,我在setFactory方法里面调用getFactory是可以获取到的,但是在测试类中就不行了,我看有人说是web项目,我就把项目发到tomcat里面去,运行起来,但还是不成功。
输出如下:
注入SessionFactory.............
注入的SessionFactory不为空.............
注入的SessionFactory:org.hibernate.impl.SessionFactoryImpl
######################################
获取SessionFactory.............
SessionFactory不为空:org.hibernate.impl.SessionFactoryImpl
######################################
初始化BaseDao的Bean.................
java.lang.NullPointerException
at cn.yesol.defens.dao.impl.BaseDao.getSession(BaseDao.java:39)
at cn.yesol.defens.dao.impl.UserDaoImpl.save(UserDaoImpl.java:18)
at cn.yesol.defens.service.impl.UserService.add(UserService.java:40)
at cn.yesol.defens.Test.Test.add(Test.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
添加用户服务层............
添加用户持久化层............
获取SessionFactory.............
SessionFactory为空.............
错误就是发生在获取SessionFactory那里,已经弄了好久了,求教,在线等,谢谢!!!
hibernate 整合 java sessionfactory获取空异常 spring
天天看飞机
9 years, 9 months ago