Search code examples
javaspringweb.xml

How do you add Java Based Spring config to an existing web.xml?


I have an existing web.xml and I am trying to add springs to a project. I would like to use java based spring config: like the following.

AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(AppConfig.class);
  

However, I don't want a WebApplicationInitializer, because I don't want to rewrite the web.xml.

Is there way to reference the AppConfig.class from the web.xml? I don't want to define all my object in xml like old spring.

Thanks


Solution

  • Using spring (without spring-boot) there is a second path used by vintage programmers of old, an anicent and a forgotten path, by defining a listener. Scolls of new recommend a 3.0 servlet-api.

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.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>aware.like.fox.crossing.water.AppConfig</param-value>
        </context-param>
    </web-app>
    

    Old black, mighty magic.

    Why do we change from xml to java. Xml is for configuration, java is for intelligence. We changed for false reasons or both is true?