下面列出了org.springframework.web.servlet.DispatcherServlet#setContextConfigLocation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Before
public void startJetty() throws Exception {
Tracee.getBackend().clear();
server = new Server(new InetSocketAddress("127.0.0.1", 0));
ServletContextHandler context = new ServletContextHandler(null, "/", ServletContextHandler.NO_SECURITY);
final DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
dispatcherServlet.setContextInitializerClasses(TraceeInterceptorSpringApplicationInitializer.class.getCanonicalName());
dispatcherServlet.setContextConfigLocation(TraceeInterceptorSpringConfig.class.getName());
context.addServlet(new ServletHolder(dispatcherServlet), "/");
server.setHandler(context);
server.start();
ENDPOINT_URL = "http://" + server.getConnectors()[0].getName() + "/";
}
@Test
public void testFrameworkServletWithCustomLocation() throws Exception {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml "
+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
}
@Test
public void testFrameworkServletWithCustomLocation() throws Exception {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml "
+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
}
@Bean
public ServletRegistrationBean apiDispatcher() {
DispatcherServlet api = new DispatcherServlet();
api.setContextClass(AnnotationConfigWebApplicationContext.class);
api.setContextConfigLocation(ApiDispatcherServletConfiguration.class.getName());
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(api);
registrationBean.addUrlMappings("/api/*"); // api下面的所有内容都访问到这里
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
registrationBean.setName("api"); // 不能重复,重复则以最后一个设置的为准
return registrationBean;
}
@Bean
public ServletRegistrationBean appDispatcher() {
DispatcherServlet api = new DispatcherServlet();
api.setContextClass(AnnotationConfigWebApplicationContext.class);
api.setContextConfigLocation(AppDispatcherServletConfiguration.class.getName());
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(api);
registrationBean.addUrlMappings("/app/*"); // app下面的所有内容都访问到这里
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
registrationBean.setName("app"); // 不能重复,重复则以最后一个设置的为准
return registrationBean;
}
@Test
public void testFrameworkServletWithCustomLocation() throws Exception {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml "
+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
}
private void initSpringMvc(ServletContext servletContext, AnnotationConfigWebApplicationContext context) {
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.setContextConfigLocation(configLocation());
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
dispatcher.addMapping("/");
}
private void initSpringMvc(ServletContext servletContext, AnnotationConfigWebApplicationContext context) {
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.setContextConfigLocation(configLocation());
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
dispatcher.addMapping("/");
}
private void initSpringMvc(ServletContext servletContext, AnnotationConfigWebApplicationContext context) {
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.setContextConfigLocation(configLocation());
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
dispatcher.addMapping("/");
}
private void initSpringMvc(ServletContext servletContext, AnnotationConfigWebApplicationContext context) {
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.setContextConfigLocation(configLocation());
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
dispatcher.addMapping("/");
}