第03章 Spring-Boot 应用 Servlet、Filter、listener 配置

第03章 Spring-Boot 应用 Servlet、Filter、listener 配置

前言

  • 为什么要使用 Spring-Boot?

    可参考官方文档。

  • 什么是 Spring-Boot?

    可参考官方文档说明。

  • 官方地址

    https://spring.io/projects/spring-boot

目标

  • 完成 Jenkins 在 Docker 中的安装与配置。
  • 安装在 Docker 中的 Jenkins 能正常对外提供服务。
  • 在外部开发环境中能正常访问和使用 Jenkins 持续集成服务。

环境

  • **VMware:**VMware Workstation 14 Pro

  • **Linux:**CentOS7.4

  • **Docker:**18.06.0-ce, build 0ffa825

  • **Jenkins:**Jenkins2.121.1

  • **JDK:**jdk1.8.0_172

  • **Spring-Boot:**2.0.4

  • **Eclipse:**Eclipse Neon.3 (4.6.3)

  • spring-tool-suite:

    Version: 3.8.4.RELEASE
    Build Id: 201703310825
    Platform: Eclipse Neon.3 (4.6.3)

  • **Maven:**3.5.0

配置简单介绍

在 Spring-Boot中针对Servlet, Filter, Listener有三种配置方式可在应用中生效,分别是:通过在启动类中以java bean方式进行依赖注入生效,另一种是通过启动类实现接口 ServletContextInitializer方式生效,最后一种是通过在启动类上增加注解 @ServletComponentScan生效。

三种配置的定义方式

在启动类中以java bean方式进行依赖注入

public class RingyinInfrastructureApplication {
    
    
	@Bean
	public ServletRegistrationBean servletRegistrationBean() {
    
    
		return new ServletRegistrationBean(new RingyinServlet(), "/ringyin");
	}

	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
    
    
		return new FilterRegistrationBean(new RingyinFilter(), 		servletRegistrationBean());
	}

	@Bean
	public ServletListenerRegistrationBean<RingyinListener> 	servletListenerRegistrationBean() {
    
    
		return new ServletListenerRegistrationBean<RingyinListener>(new RingyinListener());
	}
}

启动类实现接口 ServletContextInitializer方式

public class RingyinInfrastructureApplication implements ServletContextInitializer {
    
    
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
    
    
		servletContext.addServlet("ringyinServlet", new RingyinServlet()).addMapping("/ringyin");
		servletContext.addFilter("ringyinFilter", new RingyinFilter())
				.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "ringyinServlet");
		servletContext.addListener(new RingyinListener());
	}

	public static void main(String[] args) {
    
    
		SpringApplication.run(RingyinInfrastructureApplication.class, args);
	}
}

在启动类上增加注解 @ServletComponentScan生效

@ServletComponentScan //第三种方式使用注解启动扫描 Servlet, Filter, Listener
@SpringBootApplication
public class RingyinInfrastructureApplication {
    
    
	public static void main(String[] args) {
    
    
		SpringApplication.run(RingyinInfrastructureApplication.class, args);
	}
}

自定义异常配置处理

自定义异常处理类

@ControllerAdvice
public class ExceptionAndErrorHandler {
    
    
	private static final Logger logger = LoggerFactory.getLogger(ExceptionAndErrorHandler.class);

	/**
	 * 500异常错误统一处理
	 * 
	 * @param exception
	 *            exception
	 * @return
	 */
	@ExceptionHandler({
    
     RuntimeException.class })
	@ResponseStatus(HttpStatus.OK)
	public ModelAndView processException(RuntimeException exception) {
    
    
		logger.info("运行系统内部异常-RuntimeException");
		ModelAndView m = new ModelAndView();
		m.addObject("ringyinException", exception.getMessage());
		m.setViewName("error/500");
		return m;
	}

	/**
	 * 非404和500异常错误统一处理
	 * 
	 * @param exception
	 *            exception
	 * @return
	 */
	@ExceptionHandler({
    
     Exception.class })
	@ResponseStatus(HttpStatus.OK)
	public ModelAndView processException(Exception exception) {
    
    
		logger.info("编译期未捕获异常-Exception");
		ModelAndView m = new ModelAndView();
		m.addObject("exception", exception.getMessage());
		m.setViewName("error/5xx");
		return m;
	}
}

404 异常配置处理

添加 404 页面文件

src/main/resources/public/error/404.html

500 异常配置处理

添加 500 页面模板文件

src/main/resources/template/error/500.ftl

5xx异常配置处理

添加 5xx 页面模板文件

src/main/resources/template/error/5xx.ftl



### 500 异常配置处理

添加 500 页面模板文件

src/main/resources/template/error/500.ftl




### 5xx异常配置处理

添加 5xx 页面模板文件

src/main/resources/template/error/5xx.ftl


猜你喜欢

转载自blog.csdn.net/pointdew/article/details/108541380