2015-05-22 59 views
0

我將使用spring mvc和hibernate。 當我啓動服務器時,「org.hibernate.HibernateException:無法獲取當前線程的事務同步會話」錯誤發生。 所以,我搜索了什麼問題。 我找到了一些東西。一個是使用cvc-complex-type.2.4.c:匹配的通配符是嚴格的,但是對元素'tx:annotation-driven'沒有聲明。

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

我加了。但是,發生了"The prefix "tx" for element "tx:annotation-driven" is not bound."錯誤。 因此,我再次搜索答案並獲得答案,然後應用它。 這就是使用xmlns:tx="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"。 但是, - cvc-complex-type.2.4.c:匹配通配符是嚴格的,但是對於元素'tx:annotation- driven'沒有發現聲明。並且無法找到架構命名空間'http:// www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd'的元素'tx:annotation-driven'的Spring NamespaceHandler發生錯誤。 當我搜索答案時,它是遞歸重複的。 我該如何解決這個問題?幫我...

這是我的servlet-context.xml。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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" 
    xmlns:tx="http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
     in the /WEB-INF/views directory --> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="kr.ac.jbnu.jinggumdari" /> 
    <context:annotation-config /> 

    <beans:bean 
     class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <beans:property name="url" 
      value="jdbc:mysql://localhost:3306/jinggumdari" /> 
     <beans:property name="username" value="root" /> 
     <beans:property name="password" value="mysql1234" /> 
    </beans:bean> 

    <!-- Hibernate 4 SessionFactory Bean definition --> 
    <beans:bean id="hibernate4AnnotatedSessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <beans:property name="dataSource" ref="dataSource" /> 
     <beans:property name="annotatedClasses"> 
      <beans:list> 
       <beans:value>kr.ac.jbnu.jinggumdari.model.Member</beans:value> 
      </beans:list> 
     </beans:property> 
     <beans:property name="hibernateProperties"> 
      <beans:props> 
       <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect 
       </beans:prop> 
       <beans:prop key="hibernate.show_sql">true</beans:prop> 
      </beans:props> 
     </beans:property> 
    </beans:bean> 

    <beans:bean id="memberDAO" class="kr.ac.jbnu.jinggumdari.DAO.MemberDAOImpl"> 
     <beans:property name="sessionFactory" 
      ref="hibernate4AnnotatedSessionFactory" /> 
    </beans:bean> 
    <beans:bean id="memberService" 
     class="kr.ac.jbnu.jinggumdari.serviceImplementation.MemberManageServiceImpl"> 
     <beans:property name="memberDAO" ref="memberDAO"></beans:property> 
    </beans:bean> 

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

    <beans:bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <beans:property name="sessionFactory" 
      ref="hibernate4AnnotatedSessionFactory" /> 
    </beans:bean> 
</beans:beans> 

回答

0

您的聲明是錯誤的。定義名稱空間和模式位置是兩件不同的事情。不要把xmlns:tx放在xis:schemaLocation之內,應該是http:

?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd"> 

在從xsi:schemaLocation短刪除xmlns:tx="

另一個提示使用無版本模式,而不是版本化模式。

+0

我試試這個。當我應用這個改變時,」The元素「tx:annotation-driven」的前綴「tx」未被綁定「error occurred。如何解決這個問題? – Rect

+0

架構位置中有一個額外的'''修正了它。還要確保你正在刪除正確的一個! –

+0

謝謝。我解決了這個問題! – Rect

1

你在你的bean聲明錯過

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

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 
+0

「xmlns:tx =」http://www.springframework.org/schema/tx「 還有最後一個屬性,是不是? – Rect

相關問題