Alexander Widera
Alexander Widera

Reputation: 1

Spring annotation configuration replacement for web.xml configuration

I want to replace the configuration of spring contexts in web.xml by an annotation based configuration.

What is the best approach to keep the configuration the same way it is, but no more the separate xml config files in web.xml.

Currently each listed context is loaded after each other, to have some "boot phases". Then there are context files that fill the application context (shared by every servlet) and servlet specific xml configuration files.

The starting point should be a single reference in web.xml to the initial RootContext java class with @Configuration annotation. But how to proceed then, to keep the separate contexts, as they are now?

Here the current web.xml snippet:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            /WEB-INF/spring/2nd-context.xml
            /WEB-INF/spring/3rd-context.xml
            /WEB-INF/spring/4th-context.xml
        </param-value>
    </context-param>

<servlet>
        <servlet-name>springWebapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/servlet/servlet-context1.xml
                /WEB-INF/spring/servlet/servlet-context2.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

I'm very grateful for help.

Upvotes: 0

Views: 6003

Answers (1)

Anudeep Gade
Anudeep Gade

Reputation: 1395

Replace

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            /WEB-INF/spring/2nd-context.xml
            /WEB-INF/spring/3rd-context.xml
            /WEB-INF/spring/4th-context.xml
        </param-value>
    </context-param>

By

@Configuration
@ImportResource({
  "classpath:WEB-INF/spring/root-context.xml",
  "classpath:WEB-INF/spring/2nd-context.xml",
  "classpath:WEB-INF/spring/3rd-context.xml",
  "classpath:WEB-INF/spring/4th-context.xml"
})
public class AppConfig {
}

OR you can separate configs into separate classes and importClass in AppConfig.

@Configuration
@ImportResource({
  "classpath:WEB-INF/spring/2nd-context.xml",
  "classpath:WEB-INF/spring/3rd-context.xml",
  "classpath:WEB-INF/spring/4th-context.xml"

})
public class AppSecondConfig {
}

@Configuration
@ImportResource({
  "classpath:WEB-INF/spring/root-context.xml"
})
@Import(AppSecondConfig.class)
public class AppConfig {
}

EDIT: For multiple imports in order - @Import({ AppSecondConfig.class, AppThirdConfig.class })

Replace

<servlet>
        <servlet-name>springWebapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/servlet/servlet-context1.xml
                /WEB-INF/spring/servlet/servlet-context2.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

By

@Configuration
@ImportResource({
  "classpath:WEB-INF/spring/servlet/servlet-context1.xml",
  "classpath:WEB-INF/spring/servlet/servlet-context2.xml"
})
public class ServletConfig {
}

And tell your web.xml now to refer the config classes like this

<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...config.AppConfig</param-value>
  </context-param>

 <servlet>
      <servlet-name>spring-mvc-dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextClass</param-name>
          <param-value>
              org.springframework.web.context.support.AnnotationConfigWebApplicationContext
          </param-value>
      </init-param>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>com....config.ServletConfig</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

Upvotes: 4

Related Questions