用于web.xml配置的Spring注释配置替换

时间:2015-08-19 09:03:28

标签: spring configuration annotations

我想通过基于注释的配置替换web.xml中的spring上下文配置。

保持配置的最佳方法是什么,但不再是web.xml中单独的xml配置文件。

目前,每个列出的上下文都是相继加载的,以便有一些"启动阶段"。 然后是上下文文件,它们填充应用程序上下文(由每个servlet共享)和特定于servlet的xml配置文件。

起点应该是web.xml中带有@Configuration批注的初始RootContext java类的单个引用。但是,如何继续,保持单独的上下文,就像现在一样?

这是当前的web.xml片段:

<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>

我非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

替换

<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>

通过

@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 {
}

或者你可以将配置分成单独的类和AppConfig中的importClass。

@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 {
}

编辑:按顺序进行多次导入 - @Import({AppSecondConfig.class,AppThirdConfig.class})

替换

<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>

通过

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

现在告诉你的web.xml引用像这样的配置类

<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>