1. 程式人生 > >mvc-dispatchar-servlet.xml文件報錯

mvc-dispatchar-servlet.xml文件報錯

-h val odi ges efault posit framework 數值 type

<?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

mvc-dispatchar-servlet.xml文件報錯