Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:为什么在使用注释时需要applicationContext.xml?_Java_Xml_Spring_Spring Mvc_Annotations - Fatal编程技术网

Java Spring:为什么在使用注释时需要applicationContext.xml?

Java Spring:为什么在使用注释时需要applicationContext.xml?,java,xml,spring,spring-mvc,annotations,Java,Xml,Spring,Spring Mvc,Annotations,我是春季新手 我试图使用cargo在tomcat上部署我的war文件,它抱怨在目录中找不到applicationContext.xml 我进一步查找了它应该去哪里,并添加了一个空白的xml文件applicationContext.xml,以查看它是否有效,但随后它抱怨说 [INFO] [talledLocalContainer] SEVERE: Exception sending context initialized event to listener instance of class or

我是春季新手

我试图使用cargo在tomcat上部署我的war文件,它抱怨在目录中找不到
applicationContext.xml

我进一步查找了它应该去哪里,并添加了一个空白的xml文件
applicationContext.xml
,以查看它是否有效,但随后它抱怨说

[INFO] [talledLocalContainer] SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
[INFO] [talledLocalContainer] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
我不知道为什么我需要
applicationContext.xml
,如果我真的需要它,我应该在里面放什么

我正在尽可能多地使用
@annotations
,我的单元测试也在正确地通过,这意味着,我看到我的bean确实正确地连接起来了

老人们,请帮忙

多谢各位

更新

src/main/webapp/WEB-INF/WEB.xml

<xml>
</xml>
<beans></beans>
您必须“告诉”应用程序在何处查找描述应用程序上下文的文件,如下所示:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/applicationContext.xml</param-value>
</context-param>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="your.package.here"/>

    <tx:annotation-driven />
</beans>
如果您不需要Spring事务管理,可以随意删除最后一行和相关的模式引用

另外,我想指出的是,从技术上讲,是彻底摆脱
applicationContext.xml
的一种方法,但我真的怀疑这样做的价值。您可能也会发现这一点很有用。

请查看参考文档,尤其是以下短语:

侦听器检查contextConfigLocation参数。如果参数不存在,则 侦听器使用/WEB-INF/applicationContext.xml作为默认值


这就是它寻找applicationContext.xml文件的地方,以及为什么。

当你说cargo时,你的意思是?您还可以展示您的Java配置和web.xml吗?web.xml怎么样(或者您正在使用Servlet 3初始值设定项)?糟糕,我用
web.xml
而不是
beans.xml
。我没有任何
beans.xml
好的,您是在使用
WebApplicationInitializer
还是
WebMVCConfigureAdapter
?如果是这样的话,实现/扩展这些功能的类的内容是什么