@Component,@ComponentScan,@MapperScan注解

这三个注解通常用于在 Spring 框架中配置和启用组件扫描和数据库映射。让我们逐个解释它们的作用:

1. @Component

@Component
public class Main {
    // class implementation
}
  • @Component 是 Spring 框架的核心注解之一,它用于将 Java 类标识为 Spring 托管的组件。被 @Component 注解标记的类会被 Spring 自动扫描并注册为 Spring 应用上下文中的 Bean。这样,你就可以在应用程序的其他部分使用这些 Bean。

2. @ComponentScan

@ComponentScan(basePackages = "com.example.demo")
  • @ComponentScan 注解用于配置 Spring 在启动时要扫描的包路径。它告诉 Spring 去哪些包下面查找 @Component 注解(以及其派生注解如 @Repository@Service@Controller 等),并将其注册为 Spring Bean。在上面的示例中,指定了 com.example.demo 作为扫描的根包路径,Spring 将扫描该包及其子包中的组件。

记住与@SpringBootApplication注解有冲突

3. @MapperScan

@MapperScan("com.example.demo.mapper")
  • @MapperScan 是 MyBatis 框架中的注解,用于指定 MyBatis 的 Mapper 接口所在的包路径。MyBatis 是一个持久层框架,它通过 @MapperScan 注解告诉 Spring 在启动时去指定的包路径下查找 MyBatis 的 Mapper 接口,并自动创建对应的 Mapper Bean,以便在其他地方注入和使用这些 Mapper 接口。

综上所述:

  • @Component 用于将 Java 类标记为 Spring 组件,让 Spring 可以管理和使用这些组件。
  • @ComponentScan 用于配置 Spring 在启动时要扫描的包路径,以便找到和注册标记为 @Component 的类。
  • @MapperScan 用于配置 MyBatis 框架的 Mapper 接口所在的包路径,使得这些 Mapper 接口能够在 Spring 中被正确注册和使用。

这些注解在 Spring 应用程序中通常结合使用,以便自动化地配置和管理组件和持久层的映射。

相关推荐

  1. SpringBoot注解

    2024-07-16 06:44:02       67 阅读
  2. 【Scala 】注解

    2024-07-16 06:44:02       49 阅读
  3. @Conditional注解

    2024-07-16 06:44:02       47 阅读
  4. Spring--注解

    2024-07-16 06:44:02       30 阅读
  5. SpringBoot注解

    2024-07-16 06:44:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 06:44:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 06:44:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 06:44:02       58 阅读
  4. Python语言-面向对象

    2024-07-16 06:44:02       69 阅读

热门阅读

  1. 力扣第233题“数字1的个数”

    2024-07-16 06:44:02       29 阅读
  2. 设计模式-单例模式

    2024-07-16 06:44:02       23 阅读
  3. 【Android】使用广播进行两个APP之间的通信

    2024-07-16 06:44:02       28 阅读
  4. pyppeteer 鼠标点击拖动之后如何释放鼠标

    2024-07-16 06:44:02       31 阅读
  5. Map和Set

    Map和Set

    2024-07-16 06:44:02      18 阅读
  6. day01.03.作业•二

    2024-07-16 06:44:02       22 阅读
  7. thinkphp:数据库多条件查询

    2024-07-16 06:44:02       23 阅读
  8. 数据库作业6

    2024-07-16 06:44:02       30 阅读
  9. HTTP——POST请求详情

    2024-07-16 06:44:02       23 阅读
  10. [计网初识2]web的3个核心标准html,url,http

    2024-07-16 06:44:02       27 阅读