当前位置: 首页 > news >正文

56、原生组件注入-原生注解与Spring方式注入

56、原生组件注入-原生注解与Spring方式注入

在Spring Boot中,可以通过两种方式将原生的Servlet、Filter和Listener组件注入到应用程序中:**使用原生注解**和**使用Spring方式**。

### 一、使用原生注解注入

#### 1. 原理

通过在组件类上使用`@WebServlet`、`@WebFilter`和`@WebListener`等注解,并配合`@ServletComponentScan`注解,Spring Boot会自动扫描并注册这些原生组件。

#### 2. 步骤

- **启用组件扫描**:在Spring Boot启动类上添加`@ServletComponentScan`注解,指定需要扫描的包路径。

  ```java

  @SpringBootApplication

  @ServletComponentScan(basePackages = "com.example.components")

  public class MyApplication {

      public static void main(String[] args) {

          SpringApplication.run(MyApplication.class, args);

      }

  }

  ```

- **创建原生组件**

  - **Servlet**

    ```java

    @WebServlet(urlPatterns = "/my-servlet")

    public class MyServlet extends HttpServlet {

        @Override

        protected void doGet(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

            // 处理请求

        }

    }

    ```

  - **Filter**

    ```java

    @WebFilter(urlPatterns = "/*")

    public class MyFilter implements Filter {

        // 实现过滤逻辑

    }

    ```

  - **Listener**

    ```java

    @WebListener

    public class MyListener implements ServletContextListener {

        // 监听ServletContext事件

    }

    ```

### 二、使用Spring方式注入

#### 1. 原理

通过创建配置类,使用`ServletRegistrationBean`、`FilterRegistrationBean`和`ServletListenerRegistrationBean`等类,将原生组件注册为Spring管理的Bean。

#### 2. 步骤

- **创建配置类**

  ```java

  @Configuration

  public class MyComponentConfig {

  

      @Bean

      public ServletRegistrationBean myServlet() {

          MyServlet servlet = new MyServlet();

          return new ServletRegistrationBean(servlet, "/my-servlet");

      }

  

      @Bean

      public FilterRegistrationBean myFilter() {

          MyFilter filter = new MyFilter();

          return new FilterRegistrationBean(filter, myServlet());

      }

  

      @Bean

      public ServletListenerRegistrationBean myListener() {

          MyListener listener = new MyListener();

          return new ServletListenerRegistrationBean(listener);

      }

  }

  ```

- **创建原生组件**

  不需要添加`@WebServlet`、`@WebFilter`和`@WebListener`注解,作为普通的Java类。

### 三、两种方式的区别

#### 1. 原生注解方式

- **优点**:

  - 简单直接,符合Servlet规范。

  - 组件与Spring解耦,可独立使用。

- **缺点**:

  - 无法利用Spring的依赖注入功能,组件中无法直接注入Spring管理的Bean。

#### 2. Spring方式

- **优点**:

  - 可以利用Spring的依赖注入,方便在组件中使用其他Spring Bean。

  - 便于统一管理和配置。

- **缺点**:

  - 配置相对复杂,需要编写额外的配置类。

### 四、选择建议

- 如果组件不需要依赖注入,且希望与Spring解耦,建议使用**原生注解方式**。

- 如果组件需要依赖注入,或者需要统一管理,建议使用**Spring方式**。

通过以上两种方式,可以根据实际需求灵活地将原生Servlet、Filter和Listener组件注入到Spring Boot应用程序中。


文章转载自:

http://tkUuna2n.bnygf.cn
http://lwkLSMSf.bnygf.cn
http://885fknuj.bnygf.cn
http://OpVv9Phh.bnygf.cn
http://Fq0oI038.bnygf.cn
http://ZB8vyB0t.bnygf.cn
http://uEcwn4VY.bnygf.cn
http://wIO3qaRD.bnygf.cn
http://VXk7SPbf.bnygf.cn
http://2q8R0vXU.bnygf.cn
http://zxnZlhOg.bnygf.cn
http://L916IQFk.bnygf.cn
http://zUleOgmV.bnygf.cn
http://n9Uquzfj.bnygf.cn
http://4KQHPQto.bnygf.cn
http://WPQSkIS9.bnygf.cn
http://OwMWBvpW.bnygf.cn
http://9q9GE5Lw.bnygf.cn
http://gWc4wqql.bnygf.cn
http://KEMaQ2CS.bnygf.cn
http://IMs8W76S.bnygf.cn
http://lUDYUG0o.bnygf.cn
http://BSwlVqmM.bnygf.cn
http://cqJIYeYC.bnygf.cn
http://OYIleWc5.bnygf.cn
http://u93fuAth.bnygf.cn
http://wVo6tyzG.bnygf.cn
http://uLcprlUN.bnygf.cn
http://StoTSVs6.bnygf.cn
http://W0h072zL.bnygf.cn
http://www.dtcms.com/a/245920.html

相关文章:

  • Objective-c protocol 练习
  • Wireshark 的基本使用
  • Spring Boot 项目中Http 请求如何对响应体进行压缩
  • 【Android Studio】新建项目及问题解决
  • RDMA技术详解:下一代高性能网络通信的核心
  • Karate 与Playwright的比较和融合
  • Vue 组件通信
  • SAP会计凭证抬头增强
  • MongoDB 安装实践:基于鲲鹏 ARM 架构 Ubuntu 环境
  • Linux x86_64架构下的四级分页机制详解
  • 使用 C/C++的OpenCV 将多张图片合成为视频
  • Proxy arp(代理 ARP)逻辑图解+实验详解+真机实践验证
  • 第16篇:数据库中间件多租户架构与动态数据源隔离机制
  • 社交机器人具身导航新范式!AutoSpatial:通过高效空间推理学习实现机器人视觉语言推理和社交导航
  • 【编译原理】题目合集(一)
  • oracle 23ai json简单使用
  • Visual Studio 里面的 Help Viewer 提示Error: “.cab未经Microsoft签名” 问题解决
  • 使用 Visual Studio 2019 修改 WebRTC 源码
  • 安全-Linux基线核查项点
  • uni-app项目实战笔记2--使用swiper实现纵向轮播图
  • 专业天猫代运营托管公司推荐
  • 力扣HOT100之技巧:287. 寻找重复数
  • uni-app项目实战笔记3--使用scroll-view实现每日推荐左右滑动效果
  • Arduino入门教程:1-1、先跑起来(点亮LED打印Helloworld)
  • 论文阅读:speculative decoding
  • Go语言同步原语与数据竞争:Mutex 与 RWMutex
  • Mac电脑-Office 2024 长期支持版 PPT、Excel、Word(Mac中文)
  • 基于Django的购物系统
  • 快速搭建运行Django第一个应用—投票
  • 从实验室到实践:无人机固件越权提取技术解析