how to bootstrap spring application with @Configuration within web.xml?

how to bootstrap spring application with @Configuration within old web.xml ? Say I am building a spring project with @Configuration @ComponentScan annotation style, then how do i get it working with web.xml ? where to start the config class? I did try the following code, but it doesn't seem to work.

src/main/java/com/example/springconfig/AppConfig.java

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.example.springconfig.AppConfig
        </param-value>
    </context-param>
</web-app>

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

package com.example.springconfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan("com.example")
public class AppConfig {
    public static void main(String[] args) {
        System.out.println("===========================");
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    }
}

the main method has never been invoked, any idea ?


Solution 1:

Basically you need to add ContextLoaderListener to web.xml , configure the contextClass to be AnnotationConfigWebApplicationContext and configure contextConfigLocation to be the classes of the @Configuration :

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        com.example.config.Config1,
        com.example.config.Config2
    </param-value>
</context-param>

If you have many configurations, instead of declaring all of them in the web.xml , you can consider to define just one of them in web.xml and use @Import on it to import the rest of the configuration. Something like :

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.example.config.AppConfig</param-value>
</context-param>

@Configuration
@ComponentScan("foo.bar")
@Import({Config2.class, Config3.class})
public class AppConfig  {

    

}