SpringBoot整合Java Web三大件的详细过程

 更新时间:2025年04月21日 11:21:09   作者:axinawang  
这篇文章主要介绍了SpringBoot整合Java Web三大件的详细过程,注册自定义的Servlet、Filter、Listener组件到springboot内嵌的Servlet容器,让它们发挥自己的作用,需要的朋友可以参考下

目的:注册自定义的Servlet、Filter、Listener组件到springboot内嵌的Servlet容器,让它们发挥自己的作用

使用Spring Bean 注册Java Web三大组件

路径扫描整合javaweb三大组件

1.三大组件上添加对应注解

在对应组件上分别使用@WebServlet(“/annotationServlet”)注解来映射“/annotationServlet”请求的Servlet类,

使用@WebFilter(value = {“/antionLogin”,“/antionMyFilter”})注解来映射“/antionLogin”和“/antionMyFilter”请求的Filter类,

使用@WebListener注解来标注Listener类。

@WebServlet("/annotationServlet")
public class MyServlet extends HttpServlet {
@WebFilter(value = {"/antionLogin","/antionMyFilter"})
public class MyFilter implements Filter {
@WebListener
public class MyListener implements ServletContextListener {

2.主程序启动类上添加@ServletComponentScan注解,开启基于注解方式的Servlet组件扫描支持

@ServletComponentScan
@SpringBootApplication
public class MyChapter05Application

3.测试

http://localhost:8080/annotationServlet

http://localhost:8080/antionLogin

http://localhost:8080/antionMyFilter

使用RegistrationBean注册Java Web三大件

使用组件注册方式整合Servlet

1.创建component子包及一个自定义Servlet类MyServlet,使用@Component注解将MyServlet类作为组件注入Spring容器。MyServlet类继承自HttpServlet,通过HttpServletResponse对象向页面输出“hello MyServlet”。

@Component
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().write("hello MyServlet");
    }
}

2.在config子包下创建Servlet组件配置类ServletConfig,来注册Servlet组件

@Configuration
public class ServletConfig {
    // 注册servlet组件
    @Bean
    public ServletRegistrationBean getServlet(MyServlet myServlet) {
        ServletRegistrationBean registrationBean =
                new ServletRegistrationBean(myServlet, "/myServlet");
        return registrationBean;
    }
}

3.重启项目,启动成功后,在浏览器上访问http://localhost:8080/myServlet

使用组件注册方式整合Filter

1.在component包下创建一个自定义Filter类MyFilter,使用@Component注解将当前MyFilter类作为组件注入到Spring容器中。MyFilter类实现Filter接口,并重写了init()、doFilter()和destroy()方法,在doFilter()方法中向控制台打印了“hello MyFilter”字符串。

@Component
public class MyFilter implements Filter {
	@Override 
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("hello my filter!"); 
		chain.doFilter(request, response);
	}
}

2.向Servlet组件配置类注册自定义Filter类

@Bean                                                                            
public FilterRegistrationBean getFilter(MyFilter filter){                        
	FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
	//过滤出请求路径"/toLoginPage","/myFilter",对它们特殊处理,也就是执行Filter中的方法。                 
	registrationBean.setUrlPatterns(Arrays.asList("/toLoginPage","/myFilter"));  
	return registrationBean;                                                     
}                                                                                

3、项目启动成功后,在浏览器上访问http://localhost:8080/myFilter,查看控制台打印效果

使用组件注册方式整合Listener

1.创建一个类MyListener

@Component
public class MyListener implements ServletContextListener {
    @Override 
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("contextInitialized ...");
    }
    @Override 
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("contextDestroyed ...");
    }
}

2.向Servlet组件配置类注册自定义Listener类

@Bean                                                                            
public ServletListenerRegistrationBean getServletListener(MyListener myListener){
	ServletListenerRegistrationBean registrationBean =                           
			new ServletListenerRegistrationBean(myListener);                     
	return registrationBean;                                                     
}                                                                                

3.项目启动成功后查看控制台打印效果

contextInitialized ...

4、正常关闭(保证是正常启动的)

步骤:
        ①`pom.xml`添加依赖:

<dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>

        ②配置文件`application.properties`:

            #开启所有的端点
            management.endpoints.web.exposure.include=*
            #启用shutdown
            management.endpoint.shutdown.enabled=true

        ③执行关闭请求(POST):
            在开发者工具的console输入如下代码,然后按回车:

fetch(new Request('http://localhost:8081/actuator/shutdown',{method:'POST'})).then((resp)=>{console.log(resp)})

结果:

到此这篇关于SpringBoot整合Java Web三大件的文章就介绍到这了,更多相关SpringBoot整合Java Web内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot整合Swagger页面禁止访问swagger-ui.html方式

    SpringBoot整合Swagger页面禁止访问swagger-ui.html方式

    本文介绍了如何在SpringBoot项目中通过配置SpringSecurity和创建拦截器来禁止访问SwaggerUI页面,此外,还提供了禁用SwaggerUI和Swagger资源的配置方法,以确保这些端点和页面对外部用户不可见或无法访问
    2025-02-02
  • Spring中@Cacheable注解的使用详解

    Spring中@Cacheable注解的使用详解

    这篇文章主要介绍了Spring中@Cacheable注解的使用详解,Spring框架提供了@Cacheable注解来轻松地将方法结果缓存起来,以便在后续调用中快速访问,本文将详细介绍@Cacheable注解的使用方法,并从源码级别解析其实现原理,需要的朋友可以参考下
    2023-11-11
  • Java 程序内部是如何执行的?

    Java 程序内部是如何执行的?

    这篇文章主要介绍了Java 程序内部是如何执行的,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • 基于springboot i18n国际化后台多种语言设置的方式

    基于springboot i18n国际化后台多种语言设置的方式

    这篇文章主要介绍了基于springboot i18n国际化后台多种语言设置的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Mybatis防止sql注入的实例

    Mybatis防止sql注入的实例

    本文通过实例给大家介绍了Mybatis防止sql注入的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-06-06
  • java之使用多线程代替for循环(解决主线程提前结束问题)

    java之使用多线程代替for循环(解决主线程提前结束问题)

    这篇文章主要介绍了java之使用多线程代替for循环(解决主线程提前结束问题),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 详解Java中的时区类TimeZone的用法

    详解Java中的时区类TimeZone的用法

    TimeZone可以用来获取或者规定时区,也可以用来计算时差,这里我们就来详解Java中的时区类TimeZone的用法,特别要注意下面所提到的TimeZone相关的时间校准问题.
    2016-06-06
  • 在Spring Boot使用Undertow服务的方法

    在Spring Boot使用Undertow服务的方法

    Undertow是RedHAT红帽公司开源的产品,采用JAVA开发,是一款灵活,高性能的web服务器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默认Web容器,这篇文章给大家介绍了在Spring Boot使用Undertow服务的方法,感兴趣的朋友跟随小编一起看看吧
    2023-05-05
  • 使用@Order控制配置类/AOP/方法/字段的加载顺序详解

    使用@Order控制配置类/AOP/方法/字段的加载顺序详解

    这篇文章主要介绍了使用@Order控制配置类/AOP/方法/字段的加载顺序详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java通过jersey实现客户端图片上传示例

    Java通过jersey实现客户端图片上传示例

    本篇文章主要介绍了Java通过jersey实现客户端图片上传示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03

最新评论