如何编写Spring ApplicationContext.xml文件?

程序员:

我正在使用Spring3.2.0版本,因此我试图在applicationContext.xml文件下面写入

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

        <!--
                This will automatically locate any and all property files you have
                within your classpath, provided they fall under the META-INF
                directory. The located property files are parsed and their values can
                then be used within application context files in the form of
                ${propertyKey}.
        -->
        <context:property-placeholder location="classpath*:*.properties" />
</beans>

但是这个xml文件在Eclipse编辑器中给出了错误

Multiple annotations found at this line:
    - cvc-elt.1: Cannot find the declaration of element 'beans'.
    - schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-
     beans-3.0.xsd', because 1) could not find the document; 
             2) the document could not be read;
             3) the root element of the 
     document is not <xsd:schema>.

所以我像这样改变了我的applicationcontext.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.0.xsd">

        <!--
                This will automatically locate any and all property files you have
                within your classpath, provided they fall under the META-INF
                directory. The located property files are parsed and their values can
                then be used within application context files in the form of
                ${propertyKey}.
        -->
        <context:property-placeholder location="classpath*:*.properties" />
</beans>

唯一的更改是您将看到xsd以前使用的声明3.0.xsd,现在3.2.0.xsd消失了,现在Eclipse编辑器问题消失了,但是现在当我将项目运行到Tomcat7中时,运行时出现异常

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-3.2.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.util.ErrorHandlerWrapper.warning(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaWarning(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) ~[catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) ~[catalina.jar:7.0.42]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) ~[catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) ~[catalina.jar:7.0.42]
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) ~[na:1.7.0_25]
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) ~[na:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_25]
    at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
12:20:23.289 [localhost-startStop-1] ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 6 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 578; cvc-elt.1: Cannot find the declaration of element 'beans'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) ~[catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) ~[catalina.jar:7.0.42]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) ~[catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) ~[catalina.jar:7.0.42]
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) ~[na:1.7.0_25]
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) ~[na:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_25]
    at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    ... 23 common frames omitted

我无法找出我在google中搜索的问题,但尚无任何解决方案,有人可以帮助我写applicationContext.xml文件吗?

用户1907906:

用这个:

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

请参阅模式索引以获取Spring XSD的列表。的XSD spring-beans可以在http://www.springframework.org/schema/beans/找到

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何验证spring applicationContext.xml文件

Spring无法从ApplicationContext xml文件加载类

没有XML Spring ApplicationContext

如何加载applicationContext.xml

如何在Web.xml中注册Spring @Configuration注释类而不是applicationContext.xml文件?

使用JPA的Spring安全性。如何配置applicationContext-security.XML文件?(使用DaoAuthenticationProvider)

无法在Spring中加载applicationContext.xml

如何关闭spring的ApplicationContext?

我将applicationContext.xml文件放在Spring项目目录中的哪里

Spring @Autowired和applicationContext.xml中的bean声明

Spring-boot会自动导入applicationContext.xml吗?

找不到适用于ApplicationContext的Spring配置XML

如何将applicationContext.xml转换为spring @Configuration类?

Java Spring多个ApplicationContext

Spring获取当前的ApplicationContext

Spring ApplicationContext Bean范围

从文件中读取值到applicationContext.xml文件中

如何从组件注入ApplicationContext?

如何注入ApplicationContext本身

Spring Framework中applicationContext.xml和spring-servlet.xml之间的区别

Grails ApplicationContext.xml grailsResourceLoader

在哪里可以找到示例applicationContext.xml文件

如何在Spring中正确关闭ApplicationContext?

如何通过 TestRule 检索 Spring ApplicationContext

如何在ServletContextListener中获取Spring ApplicationContext?

使用Spring将war文件的WEB-INF目录下的资源导入到applicationContext文件

在BeanDefinitionRegistryPostProcessor内部访问Spring ApplicationContext

Spring Boot启动ApplicationContext错误

无法打开Spring类路径资源[applicationContext.xml],因为它不存在