zoukankan      html  css  js  c++  java
  • 通配符的匹配很全面, 但无法找到元素

    关于spring配置的问题

    近日学习spring时遇到了这个问题:

    1. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from URL [file:/F: /workspace/spring/target/classes /applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 55; cvc- complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。  

    报错如下:

    1. 17:21:22,400 ERROR main context.ContextLoader:331 - Context initialization failed  
    2. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from URL [file:/F: /workspace/spring/target/classes /applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 54; cvc- complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。  
    3.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)  
    4.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)  
    5.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)  
    6.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)  
    7.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)  
    8.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)  
    9.     at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)  
    10.     at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)  
    11.     at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)  
    12.     at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)  
    13.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)  
    14.     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)  
    15.     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)  
    16.     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)  
    17.     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)  
    18.     at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)  
    19.     at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)  
    20.     at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)  
    21.     at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)  
    22.     at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)  
    23.     at org.apache.catalina.startup.Embedded.start(Embedded.java:825)  
    24.     at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)  
    25.     at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)  
    26.     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)  
    27.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)  
    28.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)  
    29.     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)  
    30.     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)  
    31.     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)  
    32.     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)  
    33.     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)  
    34.     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)  
    35.     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)  
    36.     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)  
    37.     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)  
    38.     at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)  
    39.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    40.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
    41.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
    42.     at java.lang.reflect.Method.invoke(Method.java:483)  
    43.     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)  
    44.     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)  
    45.     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)  
    46.     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)  

    查了好多资料,发现是applicationContext.xml文件有点小问题。

    报错时的文件头:

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:tx="http://www.springframework.org/schema/tx"   
    5.     xmlns:aop="http://www.springframework.org/schema/aop"  
    6.     xmlns:context="http://www.springframework.org/schema/context"   
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  
    8.     xsi:schemaLocation="  
    9.         http://www.springframework.org/schema/beans  
    10.         http://www.springframework.org/schema/beans/spring-beans.xsd">  

    改为:

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     <span style="white-space:pre">  </span>xmlns:context="http://www.springframework.org/schema/context"  
    4.     <span style="white-space:pre">  </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     <span style="white-space:pre">  </span>xmlns:aop="http://www.springframework.org/schema/aop"  
    6.     <span style="white-space:pre">  </span>xmlns:mvc="http://www.springframework.org/schema/mvc"  
    7.     <span style="white-space:pre">  </span>xmlns:jee="http://www.springframework.org/schema/jee"  
    8.     <span style="white-space:pre">  </span>xmlns:tx="http://www.springframework.org/schema/tx"  
    9.     <span style="white-space:pre">  </span>xsi:schemaLocation="  
    10.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    11.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
    12.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
    13.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
    14.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
    15.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">  

    就OK了~(*^__^*) 

    我这个原因是有些“schemaLocation”没有写。把对应的加上就可以了。其根本原因是spring启动是时候要通过相应的xsd文件来检验xml文件,找不到相应的xsd文件的时候就会报错。

    那么spring是如何加载xsd文件的呢?

    spring是默认从本地加载xsd文件的,举个栗子:我们打开spring-context的jar包,找到spring.schemas这个文件


    然后在org/springframework/context/config目录下可以找到这些xsd文件。默认加载的就是这些文件。

    spring这一点细节做得比较好,把所有版本的xsd文件都放进来了,无论在spring配置文件中写哪个版本的schemaLocation,都能找得到。如果不写schemaLocation的版本号是,默认是当前版本。

    解决办法:

    1.检查spring配置文件里有没有配相应的schema配置

    2.检查相应jar包有没有上述配置和文件

    3.在IDE中配置xsd的本地路径

    补充:

    1.关于在applicationContext.xml文件中这个schemaLocation的写法,就相当于key-Value,中间用空格隔开。后面是xsd文件的地址。

    在spring的文档中有讲,这个xsd文件可以自己定义。然后加进来就可以了。

    更多资料请看Spring文档:34. XML Schema-based configuration35. Extensible XML authoring

    2.XSD文件作用:描述了xml文件的文档结构。

    3.在eclipse中配置本地xsd文件关联:

    在eclipse:window-Preferences-XML-XML Catalog中可以配置自定义的本地的xsd文件

  • 相关阅读:
    java web报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    在cmd下执行mvn help:system 报错
    win10安装mysql5.7
    centos7装单机hadoop2.7.3
    win10装jdk
    oracle中批量修改年份和月份,但不修改时分秒
    Python 正则匹配网页内的IP地址及端口号
    python 爬取网页内的代理服务器列表(需调整优化)
    python 爬取百度url
    Python 爬取SeeBug poc
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5766231.html
Copyright © 2011-2022 走看看