美文网首页
Spring - Web

Spring - Web

作者: 33d31a1032df | 来源:发表于2017-05-07 12:10 被阅读10次

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

相关文章

网友评论

      本文标题:Spring - Web

      本文链接:https://www.haomeiwen.com/subject/fmyutxtx.html