@Component注解简介

欣喜 Spring 发布时间:2025-03-27 09:12:23 阅读数:1169 1
下文笔者讲述`@Component` 注解简介,如下所示

`@Component`注解简介

`@Component`是Spring框架中的一个核心注解
 用于将一个类标识为Spring管理组件(Bean)
 通过使用`@Component`注解
  Spring容器会在启动时自动检测并实例化这些类,
  并将它们纳入Spring管理范围

`@Component`注解基本功能

-自动检测:
   Spring 通过组件扫描(Component Scanning)
     自动检测带有 `@Component` 注解的类,
	 并将其注册为 Spring 容器中的 Bean。
-依赖注入:
   Spring 管理Bean可以通过依赖注入(Dependency Injection)相互协作
    简化对象创建和管理。
-通用注解:
   `@Component`是一个通用注解
      适用于任何类型的组件
	  Spring还提供其他更具体的注解
	    如:
		  `@Service`、`@Repository` 和 `@Controller`
		    这些注解本质上都是`@Component`特化形式

`@Component`注解示例

import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    public void doSomething() {
        System.out.println("Doing something in MyComponent");
    }
}

配置 Spring 应用上下文

为了使 Spring 能够扫描并注册 `@Component` 注解的类
 需要配置组件扫描。
 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 启用组件扫描 -->
    <context:component-scan base-package="com.example" />
</beans>

使用 Java 配置

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
 
在 Spring 容器中使用

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        // 创建 Spring 应用上下文
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // 获取 MyComponent 实例
        MyComponent myComponent = context.getBean(MyComponent.class);

        // 调用方法
        myComponent.doSomething();
    }
}

其他注解

 
  import org.springframework.stereotype.Service;

  @Service
  public class MyService {
      public void performService() {
          System.out.println("Performing service in MyService");
      }
  }
 `@Repository`
    用于标识数据访问层组件(通常用于 DAO 层)。
 
  import org.springframework.stereotype.Repository;

  @Repository
  public class MyRepository {
      public void saveData() {
          System.out.println("Saving data in MyRepository");
      }
  }
  
- `@Controller`:
   用于标识控制器组件(通常用于 MVC 架构中的控制器
  import org.springframework.stereotype.Controller;

  @Controller
  public class MyController {
      public void handleRequest() {
          System.out.println("Handling request in MyController");
      }
  } 
- `@RestController`:
   用于标识 RESTful 控制器组件 
  import org.springframework.web.bind.annotation.RestController;

  @RestController
  public class MyRestController {
      public String getResponse() {
          return "Response from MyRestController";
      }
  }
 
4. 自定义组件扫描
可以通过 `@ComponentScan` 注解的
   属性来定制组件扫描的行为
- `basePackages`:指定要扫描的包
  @ComponentScan(basePackages = "com.example")

- `basePackageClasses`:
   指定要扫描的类所在的包 
  @ComponentScan(basePackageClasses = MyComponent.class)
    `includeFilters` 和 `excludeFilters`**:用于包含或排除特定的组件。
  
  import org.springframework.context.annotation.FilterType;
  import org.springframework.stereotype.Component;

  @ComponentScan(
      basePackages = "com.example",
      includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Component.class),
      excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)
  )
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202503/8409.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者