spring boot集成了servlet容器,當咱們在pom文件中增長spring-boot-starter-web的maven依賴時,不作任何web相關的配置便能提供web服務,這還得歸於spring boot 自動配置的功能(由於加了EnableAutoConfiguration的註解),幫咱們建立了一堆默認的配置,之前在web.xml中配置,如今均可以經過spring bean的方式進行配置,由spring來進行生命週期的管理,大多數狀況下,咱們須要重載這些配置(例如修改服務的啓動端口,contextpath,filter,listener,servlet,session超時時間等)html
1. servlet配置java
當應用只有默認的servlet(即DispatcherServlet)時,映射的url爲/,存在其餘的servlet時,映射的路徑爲servlet的註冊的beanname(可經過@Component註解修改),建立方式以下:git
- @Component("myServlet")
- public class MyServlet implements Servlet{
-
- @Override
- public void destroy() {
- System.out.println("destroy...");
- }
-
- @Override
- public ServletConfig getServletConfig() {
- return null;
- }
-
- @Override
- public String getServletInfo() {
- return null;
- }
-
- @Override
- public void init(ServletConfig arg0) throws ServletException {
- System.out.println("servlet init...");
- }
-
- @Override
- public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException,
- IOException {
- System.out.println("service...");
- }
2. filter配置github
filter配置的映射是/*,建立方式以下:web
- @Component("myFilter")
- public class MyFilter implements Filter{
-
- @Override
- public void destroy() {
- System.out.println("destroy...");
- }
-
- @Override
- public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
- throws IOException,
- ServletException {
- System.out.println("doFilter...");
- arg2.doFilter(arg0, arg1);
- }
-
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- System.out.println("filter init...");
- }
-
- }<span style="font-family: Arial, Helvetica, FreeSans, sans-serif; font-size: 13.3333330154419px; line-height: 17.3333339691162px; background-color: transparent;"> </span>
3. listener配置spring
- @Component("myListener")
- public class MyListener implements ServletContextListener{
-
- @Override
- public void contextDestroyed(ServletContextEvent arg0) {
- System.out.println("contextDestroyed...");
- }
-
- @Override
- public void contextInitialized(ServletContextEvent arg0) {
- System.out.println("listener contextInitialized...");
- }
-
- }
若是以爲控制力度不夠靈活(例如你想修改filter的映射),spring boot還提供了
ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean這3個東西來進行配置
修改filter的映射json
- @Configuration
- public class WebConfig {
- @Bean
- public FilterRegistrationBean filterRegistrationBean(MyFilter myFilter){
- FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
- filterRegistrationBean.setFilter(myFilter);
- filterRegistrationBean.setEnabled(true);
- filterRegistrationBean.addUrlPatterns("/bb");
- return filterRegistrationBean;
- }
- }
4. 配置servlet 容器springboot
能夠經過兩種方式配置servlet容器,一種是經過properties文件,例如:session
- server.port=8081
- server.address=127.0.0.1
- server.sessionTimeout=30
- server.contextPath=/springboot
完整的配置信息能夠看這
http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java
另外一種方式是java代碼的形式:app
- @Component
- public class MyCustomizationBean implements EmbeddedServletContainerCustomizer {
-
- @Override
- public void customize(ConfigurableEmbeddedServletContainer container) {
- container.setContextPath("/sprintboot");
- container.setPort(8081);
- container.setSessionTimeout(30);
- }
-
- }
5. error 處理
spring boot 提供了/error映射來進行錯誤處理,它會返回一個json來對錯誤信息進行描述(包含了http狀態和異常信息),例如404的錯誤
固然能夠定製錯誤頁面,經過實現ErrorController接口,並將它裝配起來便可,以下:
- @Controller
- public class ErrorHandleController implements ErrorController {
-
- @Override
- public String getErrorPath() {
- return "/screen/error";
- }
-
- @RequestMapping
- public String errorHandle(){
- return getErrorPath();
- }
- }
還能夠這樣:
- @Component
- private class MyCustomizer implements EmbeddedServletContainerCustomizer {
-
- @Override
- public void customize(ConfigurableEmbeddedServletContainer container) {
- container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/screen/error"));
- }
-
- }
6.模板引擎
spring boot 會自動配置 FreeMarker,Thymeleaf,Velocity,只須要在pom中加入相應的依賴便可,例如應用Velocity
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-velocity</artifactId>
- </dependency>
默認配置下spring boot會從src/main/resources/templates目錄中去找模板
7. jsp限制
若是要在spring boot中使用jsp,得將應用打包成war,這裏有配置的example https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp