WebApplicationInitializer 接口
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebConfig.class);
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.example")
public class WebConfig {
}
RestController
@RestController
public class HelloRestController {
@RequestMapping("/hello")
public String hello() {
return "Hello World!!!";
}
}
完整示例:GitHub
PS:本文使用的是spring-4.3.7.RELEASE
网友评论