0

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 ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
feiyuerenhai
  • 308
  • 1
  • 3
  • 11
  • There is the Spring Boot Legacy project for that see https://github.com/dsyer/spring-boot-legacy. But you don't really need a web.xml. You can just package and run the war (you are already on ee7 according to the tags). Then just follow the instructions in the regular Spring Boot documentation on how to deploy a war (and what you need to do for that). – M. Deinum Jan 17 '22 at 13:02
  • but i am curious how to achieve that without this spring-boot-legacy and use pure spring framework only ? – feiyuerenhai Jan 17 '22 at 13:15
  • Just define a `ContextLoaderListener` as mentioned in the documentation for Spring itself. There is no `@SpringConfiguration` annotation, that I know of. – M. Deinum Jan 17 '22 at 13:18
  • sry, I meant @Configuration – feiyuerenhai Jan 17 '22 at 13:21

1 Answers1

2

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  {

    

}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Hi, can you check my latest updates, and help me find out what on earth is wrong? – feiyuerenhai Jan 18 '22 at 04:45
  • are there any exceptions ? It is normal that the main method will not be executed when it deployed to to web container as the web container will take control of the main method..... – Ken Chan Jan 18 '22 at 07:51