SpringBoot 第二篇 之 @Component ,@ComponentScan 详解

本文详细探讨了SpringBoot中用于组件扫描的@Component和@ComponentScan注解。@Component标记类加入Spring IoC容器,而@ComponentScan指定扫描策略,包括默认扫描当前包及子包、自定义包名、过滤条件等。文中通过源码分析和示例代码展示了其工作原理和使用方法。
摘要由CSDN通过智能技术生成

一个个Bean使用@Bean注解注入Spring IoC 很麻烦,好在Spring允许我们进行扫描装配Bean

到IoC容器中,对于扫描装配而言使用的注解是 @Component 和@ComponentScan。

@Component 是标注哪个类被扫描进入 Spring IoC 容器,

@ComponentScan 则是标明采用何种策略去扫描装配Bean。

默认 @ComponentScan 只会扫描当前包和子包

@ComponentScan 源码如下:


  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package org.springframework.context.annotation;

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Documented
public @interface ComponentScan {
    
       //定义扫描的包
    @org.springframework.core.annotation.AliasFor("basePackages")
    java.lang.String[] value() default {};

    // 定义扫描的包
    @org.springframework.core.annotation.AliasFor("value
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值