日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot —— @ComponentScan注解

發(fā)布時間:2023/12/13 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot —— @ComponentScan注解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、作用
  • 二、注解屬性說明
  • 三、使用方式


一、作用

主要是從定義的掃描路徑中,找出標識了需要裝配的類自動裝配到Spring的bean容器中。

簡單的說就是 @ComponentScan告訴Spring從哪里找到bean,一旦指定了,Spring就會將指定的包及其下級的包中尋找bean。

在SpringBoot項目中,我們并沒有顯示的看到該注解,但是仍然能掃描到bean呢?

其實,在創(chuàng)建SpringBoot項目中,默認在啟動類上添加了@SpringBootApplication注解,該注解中包含@ComponentScan注解,因此SpringBoot會自動幫我們掃描啟動類所在的包。


二、注解屬性說明

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan {/*** 對應的包掃描路徑 可以是單個路徑,也可以是掃描的路徑數(shù)組* @return*/@AliasFor("basePackages")String[] value() default {};/*** 和value一樣是對應的包掃描路徑 可以是單個路徑,也可以是掃描的路徑數(shù)組* 如果為空,則以@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。

注:如果需要掃描的類在不同的包下,最好是精確指定要掃描的包,這樣可以減少加載時間。

總結

以上是生活随笔為你收集整理的SpringBoot —— @ComponentScan注解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。