簡體   English   中英

無法找到XML架構名稱空間springframework上下文的Spring NamespaceHandler

[英]Unable to locate Spring NamespaceHandler for XML schema namespace springframework context

我正在嘗試使用maven shade Plug-in構建具有依賴性的單個可執行jar文件。在我的pom.xml中,我對本地已編譯的軟件包(mvn install)添加了依賴性,該軟件包也是基於springframework的軟件包。 但是運行應用程序時出現以下錯誤。

線程“主”中的異常org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置問題:無法為XML模式名稱空間找到Spring NamespaceHandler [ http://www.springframework.org/schema/context]令人討厭的資源:類路徑資源[applicationContext.xml]

在花一半的時間搜索答案時,我發現不同的springframework模塊的清單文件可以互相覆蓋,因此我必須使用AppendingTransformer以便可以附加它們而不是將它們覆蓋。 所以我添加了這些行,但仍然失敗。 有什么問題嗎?

這是我的插件定義。

  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.1</version>

  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <finalName>my-spring-app</finalName>
        <shadedArtifactAttached>true</shadedArtifactAttached>
        <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.mycomp.App</mainClass>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.handlers</resource>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.schemas</resource>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.tooling</resource>
          </transformer>
        </transformers>

      </configuration>
    </execution>
  </executions>
</plugin>

更新這里是我的applicationContext.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:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    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/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">



    <context:property-placeholder location="classpath*:test.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <!-- C3P0 properties -->
        <property name="acquireIncrement" value="${acquireIncrement}" />
        <property name="minPoolSize" value="${minPoolSize}" />
        <property name="maxPoolSize" value="${maxPoolSize}" />
        <property name="maxIdleTime" value="${maxIdleTime}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="default_schema">test</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

        <property name="annotatedClasses">
            <list>

                <value>com.mycomp.database.myVO</value>

            </list>
        </property>

    </bean>

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

    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

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


    <task:annotation-driven />

</beans>

Maven-shade-plugin很有氣質,以我的經驗,一旦您擁有多個帶有幾個庫jar的類,從中獲取一個好的jar可能會很麻煩。 Capsule是一個有趣的替代方法,如果需要,可以使用啟動器來設置類路徑,甚至從Maven Central下載依賴項! 有一個Maven插件,但仍處於早期開發中。

就是說,您需要使用mvn clean package強制從頭開始重建着色的jar。

我已經通過為我的spring項目設置了無xml的設置解決了我的問題。 我在@Configurations類中定義了我的spring bean,這為我解決了這個問題。

好。 我終於找到了問題。 我的pom配置不正確。

我有如下的陰影插件條目。

<plugins>
  <pluginManagement>
     <plugin>
        .... shade stuff
     </plugin>
  </pluginManagement>
</plugins>

解決方案是將其放在pluginManagement之外,如下所示。

<plugins>
  <plugin>
    ... shade stuff
  </plugin>
  <pluginManagement/>
</plugins>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM