Question

Just a general question, when you define a Java based configuration web app. Ie.e have a class for : ApplicationContext and a WebApplicationInitializer class.

How does Spring know it has to load the beans, as no xml config files exists.. how does tomcat know anything about the webapp without a web.xml

Its a newbie question.. i appreciate that. :)

Was it helpful?

Solution

See this blog post from SpringSource blog, important part about web.xml has an example, basically you point to JavaConfigWebApplicationContext instead of default XmlWebApplicationContext in DispatcherServlet's <init-param>:

<web-app>
    <!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext
         instead of the default XmlWebApplicationContext -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
    </context-param>
    <!-- Configuration locations must consist of one or more comma- or space-delimited
         fully-qualified @Configuration classes -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>example.RootApplicationConfig</param-value>
    </context-param>
    <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Declare a Spring MVC DispatcherServlet as usual -->
    <servlet>
        <servlet-name>dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext
             instead of the default XmlWebApplicationContext -->
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
        </init-param>
        <!-- Again, config locations must consist of one or more comma- or space-delimited
             and fully-qualified @Configuration classes -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>example.web.WebBeansConfig</param-value>
        </init-param>
    </servlet>
</web-app>

OTHER TIPS

I have a VERY GOOD WAY to help you learn Spring MVC if you have Maven up and running.

IF SO: go to your command line (Cygwin) I use...

  1. mvn archetype:generate
  2. It will ask for an 'archetype number'. For you... type 16
  3. Enter the group ID which is just the main package.
  4. Enter Artifact ID which is your project name.
  5. SNAP-SHOT --- just press enter and same with version.
  6. Package - is the same as your group ID name. EX: com.spring
  7. Confirm it by entering the letter 'y' and press enter.

DO all of the above after your are in your workspace directory. That way it is created there.
You can do "mvn eclipse:eclipse" to load it in Eclipse OR you can just import it. I prefer the old fashioned importing an existing project.

Everything will be 'already' set up for you in terms of ALL configuration (Java-Based) which is good for you. It will have all the Maven dependencies you need as well already in your pom.xml. You can add or take from it if you want.

The point here is that you will have a running project already and you can play with it from there. I create all my projects like this at first and erase what I don't need and add what I do and then go from there.

Good luck!!!

Anywho... add this to your web.xml. This will help you in your answer. Research this below:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top