jiangedgm
2010-02-04 22:35
浏览 343
已采纳

spring2.5.6 配置 openJpa 异常

最近在做一个小列子,原本是用配置文件方式注入的,之后我想改为用注解的方式注入,问题就出现了。
错误代码如下:
[code="java"]2010-2-4 22:18:04 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalArgumentException: entityManagerFactory or jpaTemplate is required
at org.springframework.orm.jpa.support.JpaDaoSupport.checkDaoConfig(JpaDaoSupport.java:119)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:499)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectMethods(InjectionMetadata.java:117)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:253)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:998)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:168)
at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:155)
at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:129)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:143)
at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:113)
at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:275)
at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:365)
at com.opensymphony.xwork2.DefaultActionInvocation.access$000(DefaultActionInvocation.java:38)
at com.opensymphony.xwork2.DefaultActionInvocation$1.doProfiling(DefaultActionInvocation.java:83)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.(DefaultActionInvocation.java:74)
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)[code="java"][/code]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)[/code]

java代码如下:
[code="java"]
@Repository
public class ExamDAOImpl extends JpaDaoSupport implements ExamDAO{
private String hql = "from Exam a ";
//新增试卷
public void create(Exam exam) {
this.getJpaTemplate().persist(exam);
}
//修改试卷
public void update(Exam exam) {
this.getJpaTemplate().merge(exam);
}
//删除试卷
public void delete(Exam exam) {
this.getJpaTemplate().remove(this.getJpaTemplate().merge(exam));
}
//根据id获取试卷信息
public Exam query(int id) {
Exam exam = this.getJpaTemplate().find(Exam.class, id);
return exam;
}
//获取所有试卷的信息
@SuppressWarnings("unchecked")
public List queryList() {
List list = new ArrayList();
list = this.getJpaTemplate().find(hql);
return list;
}
}

@Namespace("/")
@Results({@Result("jsp/examManagement.jsp"),
@Result(name="createQuestion", value = "jsp/questionManagement.jsp"),
@Result(name="exam", value = "jsp/exam.jsp"),
@Result(name="examResult", value = "jsp/examResult.jsp")})
public class ExamAction extends ActionSupport {
private static final long serialVersionUID = 5192939935859483841L;
//新增试卷信息
public String create() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
//获取试卷信息
Exam exam = getExamBean(req, res);
//进行新增
getExamService().create(exam);
//获取已有的试卷信息
List listExams = getExamService().queryList();
//将已有的试卷信息传递给页面
req.setAttribute("listExams", listExams);
//存放消息
req.setAttribute("msg", "新增成功");
return SUCCESS;
}
//修改试卷信息
public String update() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
//获取试卷信息
Exam exam = getExamBean(req, res);
//进行修改
getExamService().update(exam);
//获取已有的试卷信息
List listExams = getExamService().queryList();
//将已有的试卷信息传递给页面
req.setAttribute("listExams", listExams);
//存放消息
req.setAttribute("msg", "修改成功");
return SUCCESS;
}
//删除试卷信息
public String delete() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
//获取试卷信息
Exam exam = getExamBean(req, res);
//进行删除
getExamService().delete(exam);
//获取已有的试卷信息
List listExams = getExamService().queryList();
//将已有的试卷信息传递给页面
req.setAttribute("listExams", listExams);
//存放消息
req.setAttribute("msg", "删除成功");
return SUCCESS;
}
//设定该试卷下的题目
public String createQuestion() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
//获取试卷信息
Exam exam = getExamBean(req, res);
exam = getExamService().query(exam.getId());
//获取该试卷题目下已有的题目
Set listQuestions = exam.getQuestion();
//将该试卷题目下已有的题目信息传递给页面
req.setAttribute("listQuestions", listQuestions);
//将该试卷信息传递给页面
req.setAttribute("exam", exam);
//存放消息
req.setAttribute("msg", "欢迎进入" + exam.getName() + "试题题目管理系统");
return "createQuestion";
}
//进行考试
public String exam() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
//获取试卷信息
Exam exam = getExamBean(req, res);
exam = getExamService().query(exam.getId());
//将该试卷信息传递给页面
req.setAttribute("exam", exam);
//存放消息
req.setAttribute("msg", "欢迎您开始《" + exam.getName() + "》试卷");
return "exam";
}
//统计试卷成绩
public String submitExam() {
HttpServletRequest req = ServletActionContext.getRequest();
//获取试卷信息
Exam exam = new Exam();
exam = getExamService().query(Integer.valueOf(req.getParameter("examId")));
//定义该此考试的分数
float result = 0f;
//获取该试卷下的题目
Set listQuestions = exam.getQuestion();
Iterator iterator = listQuestions.iterator();
//获取每个题目下的项目
while (iterator != null && iterator.hasNext()) {
Question question = (Question)iterator.next();

//获取该题目下的项目
List listQuestionItems = question.getQuestionItem();
//获取用户选择的题目下的项目
Assert.notNull((String)req.getParameter(question.getId() + ""));
int questionId = Integer.valueOf((String)req.getParameter(question.getId() + ""));
//累计用户提交的题目下的项目
for (int j = 0; listQuestionItems != null && j < listQuestionItems.size(); j++) {
QuestionItem questionItem = (QuestionItem)listQuestionItems.get(j);
if(questionId == questionItem.getId()) {
if (questionItem.getIsOk() == 1) {
//统计成绩
result = result + question.getPoint();
break;
}
}
}
}
//将该试卷信息传递给页面
req.setAttribute("exam", exam);
//将成绩传递给页面
req.setAttribute("result", result);
//存放消息
req.setAttribute("msg", "欢迎您查看《" + exam.getName() + "》试卷的成绩");
return "examResult";
}
public String index() {
HttpServletRequest req = ServletActionContext.getRequest();
//获取已有的试卷信息
List listExams = getExamService().queryList();
//将已有的试卷信息传递给页面
//System.out.println("listExams.size==="+listExams.size());
req.setAttribute("listExams", listExams);
req.setAttribute("msg", "欢迎进入试卷题目管理系统");
return SUCCESS;
}
//获取试卷业务逻辑
public ExamService getExamService(){
return examService;
}
//设定试卷业务逻辑
@Autowired
public void setExamService(ExamService examService){
this.examService = examService;
}
private ExamService examService;
//获取试题项目业务逻辑
public QuestionItemService getQuestionItemService(){
return questionItemService;
}
//设定试题项目业务逻辑
@Autowired
public void setQuestionItemService(QuestionItemService questionItemService){
this.questionItemService = questionItemService;
}
private QuestionItemService questionItemService;
//封装试卷基本信息
public Exam getExamBean(HttpServletRequest req, HttpServletResponse res) {
Exam exam = new Exam();
try {
//判断是否是新增或者修改还是删除,如果是新增则不需要id
String hh = req.getParameter("examId" + (String)req.getParameter("checkbox") + "");
if (!"".equals(hh) && hh != null) {
exam.setId(Integer.valueOf(req.getParameter("examId" + (String)req.getParameter("checkbox") + "")));
}
//获取基本信息
exam.setName(((String)req.getParameter("name" + (String)req.getParameter("checkbox") + "")));
} catch (Exception ex) {
ex.printStackTrace();
}
return exam;
}
}
[/code]

struts和spring的配置文件如下:
[code="java"]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<!--表示可以使用动态方法调用,即userAction!delete.action的形式-->

<!--表示struts处于开发状态-->


<!--使用默认的命名空间、即根目录/,在访问的时候,直接http://localhost:8080/webExam/userAction!delete.action-->
<!--<package name="default"  extends="struts-default">
    定义动态方法调用examAction!*.action
    <action name="examAction"  class="examAction">
        <result>jsp/examManagement.jsp</result>
        <result name="createQuestion">jsp/questionManagement.jsp</result>
        <result name="exam">jsp/exam.jsp</result>
        <result name="examResult">jsp/examResult.jsp</result>
    </action>
    定义动态方法调用questionAction!*.action
    <action name="questionAction"  class="questionAction">
        <result>jsp/questionManagement.jsp</result>
        <result name="createItem">jsp/questionItemManagement.jsp</result>
    </action>
    定义动态方法调用questionItemAction!*.action
    <action name="questionItemAction"  class="questionItemAction">
        <result>jsp/questionItemManagement.jsp</result>
    </action>
 </package>-->

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">










[/code]

persistence文件如下:
[code="java"]<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="entityManager" transaction-type="RESOURCE_LOCAL">
<!--这里采用Hibernate提供的持久化类-->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!--定义持久化属性-->
    <properties>
        <!--定义方言、fetch深度、是否显示sql-->
        <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
        <property name="hibernate.max_fetch_depth" value="3" />
        <property name="hibernate.show_sql" value="true" />
        <!--定义是否自动生成表,create表示每次加载都重新生成,update表示每次加载只是更新表-->
        <property name="hibernate.hbm2ddl.auto" value="update" />
         <!--定义数据库连接的相关属性-->
        <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
        <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:FLOW" />
        <!--定义数据库连接的用户名、密码等-->
        <property name="hibernate.connection.username" value="arch" />
        <property name="hibernate.connection.password" value="arch" />
    </properties>
</persistence-unit>

[/code]

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • yonglin4605 2010-02-11 15:16
    已采纳

    变通方法,ExamDAOImpl 里覆盖setEntityManagerFactory方法
    [code="java"]
    @Autowired

    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory){

    super.setEntityManagerFactory(entityManagerFactory);

    }
    [/code]

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • yonglin4605 2010-02-08 16:56

    ExamDAOImpl 中没有注入entityManagerFactory 或者 jpaTemplate
    如果是用配置文件的话,加上一个property标签,注入entityManagerFactory 就好了。(类似与SessionFactory)
    如果是全注解的话,我也不太清楚,你查一下资料吧

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题