文章目錄
- 一、作用
- 二、注解屬性說明
- 三、使用方式
一、作用
主要是從定義的掃描路徑中,找出標識了需要裝配的類自動裝配到Spring的bean容器中。
簡單的說就是 @ComponentScan告訴Spring從哪里找到bean
,一旦指定了,Spring就會將指定的包及其下級的包中尋找bean。
在SpringBoot項目中,我們并沒有顯示的看到該注解,但是仍然能掃描到bean呢?
其實,在創建SpringBoot項目中,默認在啟動類上添加了@SpringBootApplication注解,該注解中包含@ComponentScan注解
,因此SpringBoot會自動幫我們掃描啟動類所在的包。
二、注解屬性說明
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {/*** 對應的包掃描路徑 可以是單個路徑,也可以是掃描的路徑數組* @return*/@AliasFor("basePackages")String[] value() default {};/*** 和value一樣是對應的包掃描路徑 可以是單個路徑,也可以是掃描的路徑數組* 如果為空,則以@ComponentScan注解的類所在的包為基本的掃描路徑* @return*/@AliasFor("value")String[] basePackages() default {};/*** 指定具體的掃描的類* @return*/Class<?>[] basePackageClasses() default {};/*** 對應的bean名稱的生成器 默認的是BeanNameGenerator* @return*/Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;/*** 處理檢測到的bean的scope范圍*/Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;/*** 是否為檢測到的組件生成代理* Indicates whether proxies should be generated for detected components, which may be* necessary when using scopes in a proxy-style fashion.* <p>The default is defer to the default behavior of the component scanner used to* execute the actual scan.* <p>Note that setting this attribute overrides any value set for {@link #scopeResolver}.* @see ClassPathBeanDefinitionScanner#setScopedProxyMode(ScopedProxyMode)*/ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;/*** 控制符合組件檢測條件的類文件 默認是包掃描下的 **/*.class* @return*/String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;/*** 是否對帶有@Component @Repository @Service @Controller注解的類開啟檢測,默認是開啟的* @return*/boolean useDefaultFilters() default true;/*** 指定某些定義Filter滿足條件的組件 FilterType有5種類型如:* ANNOTATION, 注解類型 默認* ASSIGNABLE_TYPE,指定固定類* ASPECTJ, ASPECTJ類型* REGEX,正則表達式* CUSTOM,自定義類型* @return*/Filter[] includeFilters() default {};/*** 排除某些過來器掃描到的類* @return*/Filter[] excludeFilters() default {};/*** 掃描到的類是都開啟懶加載 ,默認是不開啟的* @return*/boolean lazyInit() default false;
}
三、使用方式
啟動類
:
package com.springboottest;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
public class SpringbootTestApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTestApplication.class, args);}
}
啟動類在 com.springboottest
包下,那么Spring默認會自動掃描該包及其子包下的bean。
Student類
(包路徑:com.springtest,非啟動類所在的包下):
package com.springtest;import lombok.Data;
import org.springframework.stereotype.Component;@Data
@Component
public class Student {private String name;private String nickName;
}
如果想讓Student類被Spring掃描到,那么我們可在啟動類中增加如下注解:
package com.springboottest;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan(basePackages = {"com.springtest", "com.springboottest"})
// @ComponentScan(basePackages = {"com"})
public class SpringbootTestApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTestApplication.class, args);}
}
上面的代碼我們可以看到,@ComponentScan注解中設置了兩個包名("com.springtest", "com.springboottest"),這樣設置是為了精確掃描范圍,當然我們也可以使用兩個包名的共同前綴com
。
注:如果需要掃描的類在不同的包下,最好是精確指定要掃描的包,這樣可以減少加載時間。