一个个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