daydreamer
daydreamer

Reputation: 92179

Spring: why do I need applicationContext.xml when I am using annotations?

I am Spring newbie

Im trying to deploy my war file on tomcat using cargo and it complained about applicationContext.xml not being found in directory.

I further looked up where it should go and added a blank xml file applicationContext.xml to see if that works but then it complained something as

[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.

I am not sure why do I need applicationContext.xml and if I really need it, what should I put in there?

I am using @annotations as much as I could and my unit tests are also passing correctly, Meaning, I see that my beans do wire up correctly

Seniors, please help

Thank you

UPDATE

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

<xml>
</xml>

src/main/resources/applicationContext.xml

<beans></beans>

Upvotes: 3

Views: 5682

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52366

Look at this part of the reference documentation, especially this phrase:

The listener inspects the contextConfigLocation parameter. If the parameter does not exist, the listener uses /WEB-INF/applicationContext.xml as a default.

That would be where it is looking for an applicationContext.xml file and why.

Upvotes: 0

Alexey Malev
Alexey Malev

Reputation: 6531

You have to "tell" your application where to look for files that describes your application context like this:

<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>

You may need to adjust the path under <param-value> but basically that's it.

I also recommend the minimun applicationContext.xml to look like this:

<?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>

In case you do not need Spring transaction management, feel free to remove the last line and related schema reference.

Also I'd like to mention that technically there is a way to get rid of applicationContext.xml completely but I really doubt it worth so. You may find this article useful aswell.

Upvotes: 4

Related Questions