mvc-dispatchar-servlet.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:mvc="http://www.springframework.org/schema/mvc"

       xmlns:jpa="http://www.springframework.org/schema/data/jpa"

       xmlns:tx="http://www.springframework.org/schema/tx"



       xsi:schemaLocation="http://www.springframework.org/schema/beans

      
http://www.springframework.org/schema/beans/spring-beans.xsd

      
http://www.springframework.org/schema/context

      
http://www.springframework.org/schema/context/spring-context.xsd

       http://www.springframework.org/schema/mvc

      
http://www.springframework.org/schema/mvc/spring-mvc.xsd

      
http://www.springframework.org/schema/data/jpa

      
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

       http://www.springframework.org/schema/tx

      
http://www.springframework.org/schema/tx/spring-tx.xsd">



    <!--指明
controller 所在包,并扫描其中的注解-->

    <context:component-scan base-package="com.euphe.controller"/>



    <!--
静态资源(js、image等)的访问 -->

    <mvc:default-servlet-handler/>



    <!--
开启注解 -->

    <mvc:annotation-driven/>



    <!--ViewResolver
视图解析器-->

    <!--用于支持Servlet、JSP视图解析-->

    <bean
id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property
name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

        <property
name="prefix" value="/WEB-INF/pages/"/>

        <property
name="suffix" value=".jsp"/>

    </bean>



    <!--
表示JPA
Repository所在的包 -->

    <jpa:repositories base-package="com.euphe.repository"/>



    <!--
链接到persistence.xml
-->

    <bean
id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">

        <property
name="persistenceUnitName"
value="defaultPersistenceUnit"/>

    </bean>



    <!--
事务管理 -->

    <bean
id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">

        <property
name="entityManagerFactory"
ref="entityManagerFactory"/>

    </bean>



    <!--
开启事务管理注解 -->

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

error:找不到transaction-manager

问题:依赖错了

解决:之前的依赖是xmlns:tx="http://www.springframework.org/schema/cache"

后来观察别人的依赖,发现别人的依赖是代码中的/tx,这种就很无赖了,本身是自己添加进去的依赖,但添加依赖时可能会出错。

这种问题实际上经常会出现,尤其是eclipse特别贴心地帮你添加import时,但这也意味着他添加的import有可能不对。碰到明明类是对的,但具体的函数就是出不来的情况,也要考虑是不是import或依赖的类出了问题。

 

error:Failed to convert property value of type [java.lang.String] to required type [javax.persistence.EntityManagerFactory] for property 'entityManagerFactory'

问题:<tx:annotation-driven transaction-manager="transactionManager"/>,transactionManager类型不对

解决:这种问题,不要看表面,要学会从根源开始查找,以后的学习也是,这种明明是配置的问题,莫名出错时,要学会找根源。哪个是哪个的调用,调用的部分是否存在,调用的部分参数值是否有问题。因为你调用和依赖不对,你后面的配置肯定会出问题。

先是查看了transactionManager的依赖,在上面的事务管理部分,id是对的,既然是类型出了问题,那么再往后看,之前出错的部分写的是<propertyname="entityManagerFactory" value="entityManagerFactory"/>,如果这样写,后面部分的value就将"entityManagerFactory"变成了一个<value></value>中的tag,而不是一个entity

原文地址:https://www.cnblogs.com/xym4869/p/8478106.html