【问题标题】:Spring 4.2 - Dependancy Injection - http://www.springframework.org/schema/p errorSpring 4.2 - 依赖注入 - http://www.springframework.org/schema/p 错误
【发布时间】:2015-08-08 11:25:30
【问题描述】:

我正在为执行返回的 DI 做一本书的简单示例:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]
Offending resource: class path resource [META-INF/spring/app-context.xml]

app-context.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="provider" class="com.apress.prospring4.ch2.HelloWorldMessageProvider"/> 
    <bean id="renderer" class="com.apress.prospring4.ch2.StandardOutMessageRenderer" p:messageProvider-ref="provider"/>
</beans>

之前的所有练习都运行良好... 没有 DI 的 bean 的外部附加练习也可以正常工作...... 使用 Spring 4.2、Maven 3、JVM 1.8。 有什么想法吗?

编辑我已将 p:notation 更改为

<bean id="renderer" class="com.apress.prospring4.ch2.StandardOutMessageRenderer">
    <property name="messageProvider" ref="provider" />
</bean>

并删除了 p 命名空间引用,以便应用程序现在可以工作,这很好,因为我是那里的学习新手。

不过我一直想知道如何让 p 命名空间工作! 作为旁注,尽管我可以看到“http://www.springframework.org/schema/beans”,但我没有看到任何“http://www.springframework.org/schema/p”;可能是我缺乏知识。

【问题讨论】:

    标签: spring dependency-injection maven-3


    【解决方案1】:

    我在将应用程序打包到单个可执行 Jar 时遇到了这个错误(直接在 eclipse/IntelliJ 中运行时有效)。

    问题是打包过程中 META-INF 文件夹中的 spring.schemas 文件被覆盖。

    您可以通过使用 maven shade 插件,并使用 mvn clean package 构建 jar 来解决这个问题

    您需要像这样使用转换器来附加 spring 文件(并将主类更改为您自己的)

    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>tom.lightwaverf.Main</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>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2022-07-04
      • 2023-03-18
      • 1970-01-01
      • 2018-11-21
      相关资源
      最近更新 更多