多个ContextLoader *定义

时间:2018-05-29 10:16:40

标签: spring-mvc

public class SpringMVCWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { LoginApplicationConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return null;
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

public class Initializer extends AbstractHttpSessionApplicationInitializer {

   public Initializer() {
       super(Config.class); 
   }
}

如何在不制作多个ContextLoader *定义的情况下同时使用SessionManager和AbstractAnnotationConfigDispatcherServletInitializer?

1 个答案:

答案 0 :(得分:1)

在使用Spring MVC并初始化多个初始化程序时,我遇到了同样的问题。 将二者分成不同的类,并用Spring @Order(value)

对其进行注释

这将首先被初始化。 AbstractAnnotationConfigDispatcherServletInitializer将启动servlet应用程序上下文以及根应用程序上下文。

import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Order(1)
public class Init extends AbstractAnnotationConfigDispatcherServletInitializer {
  @Override protected Class<?>[] getRootConfigClasses() {
    return new Class[]{Config.class};
  }

  @Override protected Class<?>[] getServletConfigClasses() {
    return null;
  }

  @Override protected String[] getServletMappings() {
    return new String[]{"/*"};
  }
}

这将用@Order(2)进行注释这是会话初始化类AbstractHttpSessionApplicationInitializer的第二次初始化,将确保Servlet将对每个负责通过支持的自定义实现替换HttpSession实现的请求使用springSessionRepositoryFilter。由Redis more details

import org.springframework.core.annotation.Order;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Order(2)
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)
public class SessionInit extends AbstractHttpSessionApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException
  {
    super.onStartup(servletContext);
  }
}