关于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了~(*^__^*) ,注:不要拷贝上面配置,要根据自己项目缺少的schema而添加

原因:

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

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

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

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

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

解决办法:

检查spring配置文件里有没有配相应的schema配置,补充上就可以了

关于spring”通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明“的错误的更多相关文章

  1. 通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明。

    在Spring相应包导入正确的前提下,出现这个异常,是因为我们在引入命名空间的时候,没有正确引入它的DTD解析文件,以上面的context为例,解决办法如下: 在引入 xmlns:context=&q ...

  2. 通配符的匹配很全面, 但无法找到元素 'context:property-placeholder'

    解决方案就是如下: xmlns:context="http://www.springframework.org/schema/context" 同时在xsi:schemaLocat ...

  3. 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。

    错误原因: xml文件中,本来是要配置成下面这样的: http://www.springframework.org/schema/context http://www.springframework. ...

  4. Spring:通配符的匹配很全面, 但无法找到元素 XXXXX' 的声明

    问题:配置Spring的时候容易发生如题的这样一个经常性的错误,错误如下(以context为例) org.springframework.beans.factory.xml.XmlBeanDefini ...

  5. nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 56; cvc-complex-type.2.4.c通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  6. 通配符的匹配很全面, 但无法找到元素 'cache:advice' 的声明

    EB-INF\classes\spring-jdbc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineN ...

  7. idea调试SpringMvc, 出现:”通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明“错误的解决方法

    调试json格式输出,出现以下错误: HTTP Status 500 - Servlet.init() for servlet HelloDispatcher threw exception   ty ...

  8. dubbo 常见错误 通配符的匹配很全面, 但无法找到元素 'dubbo:application' java.lang.reflect.MalformedParameterizedTypeException 通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明。 Unsupported major.minor version 52.0 (unable to l

    dubbo 常见错误 1. Caused by: java.lang.reflect.MalformedParameterizedTypeException 启动时报错,原因是dubbo 依赖 spr ...

  9. springMVC:通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明

    转自:http://blog.csdn.net/tolcf/article/details/50845483 报错信息:通配符的匹配很全面, 但无法找到元素 'mvc:annotation-drive ...

随机推荐

  1. Java面试题:如何对HashMap按键值排序

    Java中HashMap是一种用于存储“键”和“值”信息对的数据结构.不同于Array.ArrayList和LinkedLists,它不会维持插入元素的顺序. 因此,在键或值的基础上排序HashMap ...

  2. thinkphp5 设置.htaccess报input file specified的解决方法

    先去检查服务器设置,这个网上方法很多就不说了,如果服务器没问题还是报这个错误的话可能和php版本有关 php5.4和以下版本的.htaccess <IfModule mod_rewrite.c& ...

  3. SpringMvc访问Controller去掉do

    只需要修改web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...

  4. java多线程练习实例

    总结: 循环的使用率蛮高,Thraed.sleep(),try-catch语句 package com.aa; public class West { public static void main( ...

  5. microtime() 测试代码执行时间,提高编码效率

    <?php $b_time = microtime(true); $a = array("); $count = ; foreach ($a as $key => $value) ...

  6. 基于angularJs+ui-router+bootstrap风格的表格生成指令

    1 /** 根据参数定制表格 * api接口: * form-model:[item1,item2,item3] * form-properties:[ * {key:'',label:'',thCl ...

  7. 使用Handler类来更新UI

    在android里面,我们经常要上网获取一些数据,然后更新UI,但获取数据是要时间的,如果在主线程里面直接使用获取数据的代码的话.整个activity就会卡在那,直至你获取到数据更新完UI才会加载完成 ...

  8. Oracle11gR2_ADG管理之恢复主库的truncate表实战

    备库开启flashback database #关闭备库的同步 SQL> alter database recover managed standby database cancel; Data ...

  9. 算法初步——two pointers

    什么是 two pointers 以一个例子引入:给定一个递增的正整数序列和一个正整数 M,求序列中的两个不同位置的数 a 和 b,使得它们的和恰好为 M,输出所有满足条件的方案. 本题的一个最直观的 ...

  10. json和pickle序列化模块

    一.json序列化模块 1.序列化:将内存数据转成字符串加以保存. 2.反序列化:将字符串转成内存数据加以读取. data = { '北京':{ '五道口':{ 'sohu':'引擎', } } } ...