@FilterRegistration和@ServletRegistration注解

前言

@FilterRegistration@ServletRegistration注解是Springboot3.5.x版本之后提供的注解,使用 @ServletRegistration 注册 Servlet,使用 @FilterRegistration注册Filter

注解使用

@FilterRegistration使用

创建一个Springboot3.5.x版本新属性,然后

java 复制代码
@Slf4j
public class TestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        log.info("TestFilter:{{}}", request.getRequestURI());

        //执行
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
less 复制代码
@Configuration(proxyBeanMethods = false)
public class FilterConfiguration {

    @Bean
    @FilterRegistration(name = "encoding-filter", urlPatterns = "/*", order = 0)
    public TestFilter encodingFilter() {
        return new TestFilter();
    }
    

}

输出结果为

@ServletRegistration使用

scala 复制代码
public class MyServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getOutputStream().println("hello world");
    }
}
less 复制代码
@Configuration(proxyBeanMethods = false)
public class FilterConfiguration {
    


    @Bean
    @ServletRegistration(name = "encoding-filter1", order = 0)
    public MyServlet myServlet() {
        return new MyServlet();
    }

}

总结

Springboot3.5.x版本之后,更新了许多优秀属性,可以用用看

相关推荐
飞翔的佩奇23 分钟前
基于SpringBoot+MyBatis+MySQL+VUE实现的经方药食两用服务平台管理系统(附源码+数据库+毕业论文+部署教程+配套软件)
数据库·vue.js·spring boot·mysql·毕业设计·mybatis·经方药食两用平台
04Koi.3 小时前
八股训练--Spring
java·后端·spring
纯洁的小魔鬼4 小时前
Springboot 配置 doris 连接
spring boot·doris·连接池
c_zyer4 小时前
Mermaid流程图可视化系统:基于Spring Boot与Node.js的三层架构实现
spring boot·node.js·流程图·mermaid
Livingbody4 小时前
【心理咨询师数字孪生对话数据集】标准化为 ShareGPT OpenAI 格式
后端
jllllyuz5 小时前
Spring中的事务是如何实现的
数据库·sql·spring
wangmengxxw5 小时前
Spring-常用注解
java·数据库·spring·注解
一只鹿鹿鹿5 小时前
【网络安全】信息网络安全建设方案(WORD)
人工智能·安全·spring·web安全·低代码
_码农121385 小时前
spring boot + mybatis + mysql 只有一个实体类的demo
spring boot·mysql·mybatis
AQin10126 小时前
IP 🆚 MAC,你分得清吗?
后端·网络协议