Struts2 Spring JPA 整合时报错:No bean named 'entityManagerFactory' is defined ,请问各位是怎么解决

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2 Spring JPA 整合时报错:No bean named 'entityManagerFactory' is defined ,请问各位是怎么解决相关的知识,希望对你有一定的参考价值。

JPA过滤器组件OpenEntityManagerInViewFilter我也加了还是报这样的错误,请大侠给我解决下。。。

Struts2 Spring JPA 整合时报错,是因为没有在相应的实现类中追加相应的注解。解决方法如下:

1、调用一个普通的java类,按照spring 注解的使用方式进行了如下配置。

2、上述类就是数组件调用的类,jsp 描写方式 如下。

3、框架还有个接口 service 和实现类,想把接口注入到QueryFLTree中时,做了如下配置。

4、遵循着一个spring 的机制来排查问题。

5、加了@Autowired和 @Service @Component注解都可以是spring的bean。使用getbean 都能取出来直接用。

参考技术A

Struts2 Spring JPA 整合时报错:No bean named 'entityManagerFactory' is defined是因为没有带上包名整合后struts2中的action属性写上类名,步骤如下:

1、首先就是在web.xml中配置spring的监听器,代码如图所示,注意将代码放在文件的最上面,这是因为spring初始化容器要先执行即可。



2、接着就是进行添加struts2-spring-plugin这个jar包即可。



3、然后就是struts2与spring不整合相互之间也能工作,但是整合之后可以将struts2中的对象交给spring容器管理,否则struts2中的对象属性因为不在容器中而无法管理如下图所示。



4、最后一步就是进行整合前struts2中的action属性中的class变量必须写类的全路径,即带上包名整合后struts2中的action属性写上类名即可,如下图所示。


参考技术B 你的Spring的配置文件中是不是有下面这段配置呢?
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 此种方式不推荐,对jpa的侵入性太强,除非数据库连接池也在spring中配置 <property name="dataSource"
ref="dataSource" /> -->
<property name="persistenceUnitName" value="Jweb" />
</bean>
<!-- 自动装载EntityManager -->
<context:annotation-config />
<!-- 配置事务管理器 -->
<bean id="entityManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

如果有的话,看下jar包是否缺少了。看下这个4个是不是有的

spring-orm-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-entitymanager-3.6.0.Final.jar
参考技术C 你的Spring的配置文件中是不是有下面这段配置呢?
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 此种方式不推荐,对jpa的侵入性太强,除非数据库连接池也在spring中配置 <property name="dataSource"
ref="dataSource" /> -->
<property name="persistenceUnitName" value="Jweb" />
</bean>
<!-- 自动装载EntityManager -->
<context:annotation-config />
<!-- 配置事务管理器 -->
<bean id="entityManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

如果有的话,看下jar包是否缺少了。看下这个4个是不是有的

spring-orm-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-entitymanager-3.6.0.Final.jar追问

嗯,确实 ,和你配的一模一样,我没有spring-orm-3.0.5.RELEASE.jar
请问这个包是必须的吗?能给我发一份整合所需的所有JAR包吗?邮箱:122600365@qq.com
非常谢谢。。

追答

spring 里面使用JPA的话 这个包好像是需要的。 我一直使用的是seam框架,spring没有怎么用过所以我这也没有整合好的工程

追问

嗯,虽然没解决到,但是非常谢谢。。。

本回答被提问者采纳
参考技术D 看看是不是拼写错了bean的名字,或者注入了没有

以上是关于Struts2 Spring JPA 整合时报错:No bean named 'entityManagerFactory' is defined ,请问各位是怎么解决的主要内容,如果未能解决你的问题,请参考以下文章

SSH整合所需包

整合SSM时报错:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getT

Spring整合Mybatis,访问数据时报错:Invalid bound statement (not found)

spring整合hibernate时报错:org.hibernte.engine.transaction.spi.transactioncontext

SSH整合总结(OA项目)

guice整合struts2与jpa,guice的使用