Spring中的一个问题,大家来讨论一下..

fiyaa 2010-10-23 12:33:24

package com.jyjy.dao.impl;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

/**
* 文件:hibernateTemplateDao
* 说明:持久层所有实现类的父类,包含所有持久层实现类要使用的资源和公共方法
*
* 创建时间:2010-10-28
* 修改时间:2010-10-28
*/
@Repository("courseDao")//加上Annotation一切正常
public class SuperDao {
private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}

import org.springframework.stereotype.Repository;

import com.jyjy.dao.CourseDao;
import com.jyjy.model.Course;
import com.jyjy.util.Page;
/*
* 文件:CourseDaoImpl.java
* 说明:课程数据访问实现,从SuperDao继承避免多个Dao实现注入HibernateTemplate
*
* 创建日期:2010-10-28
* 修改时间:2010-10-28
*/
@Repository("courseDao")//加上Annotation一切正常
public class CourseDaoImpl extends SuperDao implements CourseDao {

@Override
public void delete(Course course) {
// TODO Auto-generated method stub

}

@Override
public boolean deleteByName(String name) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean existsName(String name) {
// TODO Auto-generated method stub
return false;
}

@Override
public Course loadByName(String name) {
// TODO Auto-generated method stub
return null;
}

@Override
public List<Course> loadByPage(Course course) {
// TODO Auto-generated method stub
return null;
}

@Override
public int loadByPageGetCount(Page page) {
// TODO Auto-generated method stub
return 0;
}

@Override
public void save(Course course) {
if (course != null) {
getHibernateTemplate().save(course);
}
}

@Override
public void update(Course course) {
// TODO Auto-generated method stub

}


}
以下是XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<!--
**文件:beans.xml
**
**说明:全部使用XML配置...
-->
<context:component-scan base-package="com.jyjy.dao" />

<tx:annotation-driven transaction-manager="txManager" />
<!-- 数据源的连接 -->
<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/mis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true" />
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mis" />-->
<property name="username" value="root" />
<property name="password" value="xxf84340123" />
</bean>

<!-- 将数据源连接传给Hibernate 并对Hibernate进行一些初始化的工作-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.jyjy.model</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.current_session_context_class=thread
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>

<!-- 对Hibernate进行事务管理 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 这里定义的是Hibernate的模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 定义一个切面用于实现事务 -->
<!-- -->
<aop:config>
<aop:pointcut id="businessService"
expression="execution (public * com.jyjy.service.*.*(..))" />
<aop:advisor pointcut-ref="businessService" advice-ref="txAdvice" />
</aop:config>

<!-- 事务管理 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>

<!-- 所有Dao父类SuperDao的注入-->
<bean id ="superDao" class="com.jyjy.dao.impl.SuperDao" scope="prototype">
<property name="hibernateTemplate" ref="hibernateTemplate" ></property>
</bean>


<!-- CourseDao与CourseService的注入 -->
<bean id="courseDao" class="com.jyjy.dao.impl.CourseDaoImpl">

</bean>

<bean id="courseService" class="com.jyjy.service.impl.CourseServiceImpl">
<property name="courseDao" ref="courseDao"></property>
</bean>



问题是这样的,我想写一个父类简化Dao层的hibernateTemplate, 所有的Dao都从那superDAO继承...
在Beans.xml中我进行配置后,用Juit作测试报
java.lang.NullPointerException
at com.jyjy.dao.impl.CourseDaoImpl.save(CourseDaoImpl.java:59)
at com.jyjy.service.impl.CourseServiceImpl.add(CourseServiceImpl.java:17)
at com.yang.service.impl.CourseServiceTets.testAdd(CourseServiceTets.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

但同样的测试,我用Annotation@Repository却一切正常...是不是这个Spring对@Repository做了一些特殊的处理...
我查看了文档,好像说异常处理时有一些不一样,英语不是特别好...哪位朋友,能给解释一下,为什么用XML配置会出空指针错,而用Annotation却又一切正常?谢谢....
...全文
110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fiyaa 2010-10-24
  • 打赏
  • 举报
回复
我的的意愿就是不要每次都让hibernateTemplate注入到courseDao
所以我才从superDao继承...
问题是我用ANNOTATION,我继承superDao可以正常使用而我用xml这样配置,为什么会报空指针?
fiyaa 2010-10-24
  • 打赏
  • 举报
回复
上去...
不善^ 2010-10-23
  • 打赏
  • 举报
回复
 <!-- 所有Dao父类SuperDao的注入-->
<bean id ="superDao" class="com.jyjy.dao.impl.SuperDao" scope="prototype">
<property name="hibernateTemplate" ref="hibernateTemplate" ></property>
</bean>


<!-- CourseDao与CourseService的注入 -->
<bean id="courseDao" class="com.jyjy.dao.impl.CourseDaoImpl">

</bean>

<bean id="courseService" class="com.jyjy.service.impl.CourseServiceImpl">
<property name="courseDao" ref="courseDao"></property>
</bean>



是这块的问题吧

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧