【spring】@Component注解学习

@Component介绍

@Component 是 Spring 框架中的一个注解,用于将一个类标记为 Spring 上下文中的一个组件。当一个类被标记为 @Component 时,Spring 容器会在启动时自动扫描并实例化这个类,并将其注册到 Spring 上下文中。

@Component 注解可以用于任何类,包括控制器、服务、DAO 等。当一个类被标记为 @Component 时,它就成为了 Spring 上下文中的一个 bean,可以被其他 bean 通过依赖注入的方式使用。

除了 @Component 注解之外,Spring 框架还提供了其他一些类似的注解,例如 @Controller、@Service、@Repository 等,它们分别用于标记控制器、服务、DAO 等不同类型的组件。这些注解都是 @Component 的子注解,它们的作用和 @Component 类似,只是它们在 Spring 上下文中的作用域和生命周期可能会有所不同。

在 org.springframework.stereotype下还有这些:

@Component源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {


	String value() default "";
}
源代码截图

org.springframework.stereotype下其他源码
@Controller
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {


	@AliasFor(annotation = Component.class)
	String value() default "";

}
@Repository
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {


	@AliasFor(annotation = Component.class)
	String value() default "";

}
@Service
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {


	@AliasFor(annotation = Component.class)
	String value() default "";

}
@Indexed
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Indexed {
}

@Component属性介绍

  • value:用于指定注入容器时Bean的id。如果没有指定Bean的id,默认值为当前类的名称。

@Component注解使用场景

  1. 自动组件扫描:当你使用@ComponentScan注解指定了扫描路径后,Spring会自动扫描这些路径下的类,并将所有带有@Component及其衍生注解(如@Service@Repository@Controller)的类注册为Spring容器中的Bean。这样做可以避免手动在XML配置文件中声明每一个Bean,从而简化了配置。

  2. 依赖注入@Component注解的类可以利用Spring的依赖注入机制。这意味着你可以在这些类中通过构造函数、Setter方法或者字段注入它们所依赖的其他Bean。

  3. 自定义Bean:开发者可以通过自定义注解来标记特定的组件。例如,如果你有一个@MyCustomComponent注解,你可以在配置类中声明这个注解,并将其与@Component注解一起使用,以便Spring容器能够识别并管理这些自定义组件。

  4. 条件化配置:通过结合@Conditional注解,@Component可以实现条件化的Bean创建。这意味着你可以根据不同的条件(如环境属性、系统属性等)来控制某些Bean是否被创建。

  5. 生命周期管理@Component注解的类可以利用Spring容器提供的生命周期管理功能。你可以实现InitializingBeanDisposableBean接口或者使用@PostConstruct@PreDestroy注解来自定义Bean的初始化和销毁过程。

  6. 服务层、数据访问层和控制器:虽然@Service@Repository@Controller都是@Component的特化,但它们各自用于标记不同的层次。@Service用于标记业务层组件,@Repository用于数据访问层(DAO)组件,而@Controller用于Web层的控制器。这种分层的标记有助于代码的组织和维护。

  7. 提高组件扫描效率:在Spring 5.0中引入的@Indexed元注解可以与@Component一起使用,用于在项目编译打包时生成META-INF/spring.components文件,建立索引,从而提高组件扫描效率,减少应用启动时间。

@Component测试示例代码

示例代码 一
ComponentDemo类
package com.yang.SpringTest.annotation.componentLearn;

import org.springframework.stereotype.Component;

/**
 *
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.componentLearn
 * Ceate Time 2024-03-22 11:42
 */
@Component
public class ComponentDemo {
}

ControllerDemo类
package com.yang.SpringTest.annotation.componentLearn;

import org.springframework.stereotype.Controller;

/**
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.componentLearn
 * Ceate Time 2024-03-22 11:41
 */
@Controller
public class ControllerDemo {
}

RepositoryDemo类
package com.yang.SpringTest.annotation.componentLearn;

import org.springframework.stereotype.Repository;

/**
 *
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.componentLearn
 * Ceate Time 2024-03-22 11:41
 */
@Repository
public class RepositoryDemo {
}

ServiceDemo类
package com.yang.SpringTest.annotation.componentLearn;

import org.springframework.stereotype.Service;

/**
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.componentLearn
 * Ceate Time 2024-03-22 11:41
 */
@Service
public class ServiceDemo {
}

ComponentConfig配置类
package com.yang.SpringTest.annotation.componentLearn;

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

/**
 * 配置类
 *
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.componentLearn
 * Ceate Time 2024-03-22 11:47
 */
@Configuration
@ComponentScan(value = {"com.yang.SpringTest.annotation.componentLearn"})
public class ComponentConfig {
}

ComponentTest测试类
package com.yang.SpringTest.annotation.componentLearn;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

/**
 * <p>测试类</p>
 *
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.componentLearn
 * Ceate Time 2024-03-22 11:48
 */
public class ComponentTest {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ComponentConfig.class);
        String[] definitionNames = context.getBeanDefinitionNames();
        Arrays.stream(definitionNames).forEach((definitionName) -> System.out.println(definitionName));
    }
}

运行结果




  • 18
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值