詳解Spring系列之@ComponentScan自動(dòng)掃描組件
無(wú)注解方式component-scan使用
之前,我們需要掃描工程下一些類上所標(biāo)注的注解,這些常用注解有:
@Controller,@Service,@Component,@Repository
通過(guò)在Spring的配置文件中配置<context:component-scan>掃描對(duì)應(yīng)包下掃描這些注解的方式:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--@Controller,@Service,@Component,@Repository--> <context:component-scan base-package="com.jektong.spring"/> </beans>
注解方式@ComponentScan使用
建三個(gè)類,依次將
@Controller,@Repository,@Service,標(biāo)注這些類:
圖1
現(xiàn)在通過(guò)使用注解@ComponentScan的方式來(lái)掃描所在包下面的這些類:之前定義的PersonConfig修改:
package com.jektong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import com.jektong.spring.Person; @Configuration @ComponentScan("com.jektong") public class PersonConfig { @Bean("person01") public Person person() { return new Person("李四",21); } }
測(cè)試,看是否掃描到這些注解所標(biāo)注的類:PersonTest.java
@Test public void test02() { ApplicationContext ac = new AnnotationConfigApplicationContext(PersonConfig.class); Person bean = ac.getBean(Person.class); System.out.println(bean); String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for (String string : beanDefinitionNames) { System.out.println(string); } }
測(cè)試效果:除了Spring要自動(dòng)加載的配置類以外也顯示了剛才添加的配置類:
圖2
為何會(huì)出現(xiàn)PersonConfig,因?yàn)锧Configuration本 身就是@Component注解的:
圖3
@ComponentScan的掃描規(guī)則
如果需要指定配置類的掃描規(guī)則,@ComponentScan提供對(duì)應(yīng)的掃描方式@Filter進(jìn)行配置類的過(guò)濾:
// 掃描包的時(shí)候只規(guī)定掃描一些注解配置類。 Filter[] includeFilters() default {}; // 掃描包的時(shí)候可以排除一些注解配置類。 Filter[] excludeFilters() default {};
Filter其實(shí)也是一個(gè)注解,相當(dāng)于@ComponentScan的子注解,可以看圖4:
圖4
Filter對(duì)應(yīng)的過(guò)濾規(guī)則如下:
第一種:掃描包的時(shí)候只規(guī)定掃描一些注解配置類【includeFilters】。
使用這個(gè)includeFilters過(guò)濾規(guī)則,必須解除默認(rèn)的過(guò)濾規(guī)則,
使用【useDefaultFilters = false】:
package com.jektong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import com.jektong.spring.Person; @Configuration @ComponentScan(value = "com.jektong",includeFilters = { @Filter(type = FilterType.ANNOTATION,value= {Controller.class}) },useDefaultFilters = false ) public class PersonConfig { @Bean("person01") public Person person() { return new Person("李四",21); } }
這樣就只會(huì)掃描用@Controller,標(biāo)注的配置類交給Spring容器中了:
圖5
第二種:掃描包的時(shí)候可以排除一些注解配置類【excludeFilters】。
圖6
@Filter看上圖,有5種不同類型的過(guò)濾策略。拿第一種舉例,我們需要過(guò)濾使用@Controller注解的配置類:
package com.jektong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import com.jektong.spring.Person; @Configuration @ComponentScan(value = "com.jektong",excludeFilters = { @Filter(type = FilterType.ANNOTATION,value= {Controller.class}) } ) public class PersonConfig { @Bean("person01") public Person person() { return new Person("李四",21); } }
測(cè)試看一下發(fā)現(xiàn)圖2中的personController不會(huì)交給Spring容器去管理了:
圖7
上面的圖6展示出5種不同類型的過(guò)濾策略,上面介紹了注解類型(FilterType.ANNOTATION),還有四種:
重點(diǎn)看一下CUSTOM自定義掃描策略。
從源碼看,自定義掃描注解類型需要實(shí)現(xiàn)TypeFilter接口,下面就寫(xiě)一個(gè)實(shí)現(xiàn)類MyFilter.java:在實(shí)現(xiàn)類中可以自定義配置規(guī)則:
package com.jektong.config; import java.io.IOException; import org.springframework.core.io.Resource; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.ClassMetadata; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.TypeFilter; public class MyFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { // 查看當(dāng)前類的注解。 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); // 查看當(dāng)前掃描類的信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); // 獲取當(dāng)前類資源 Resource resource = metadataReader.getResource(); String className = classMetadata.getClassName(); System.out.println("className===>" + className); // 只要類名包含er則注冊(cè)Spring容器 if(className.contains("er")) { return true; } return false; } }
測(cè)試:
PersonConfig 中進(jìn)行掃描:
package com.jektong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import com.jektong.service.PersonService; import com.jektong.spring.Person; @Configuration @ComponentScan(value = "com.jektong",includeFilters = { @Filter(type = FilterType.CUSTOM,value= {MyFilter.class}) },useDefaultFilters = false ) public class PersonConfig { @Bean("person01") public Person person() { return new Person("李四",21); } }
可以看出掃描出包下面的類只要帶“er”的全部掃描出來(lái),并配置給Spring容器:
ASSIGNABLE_TYPE:按照指定的類型去加載對(duì)應(yīng)配置類:
package com.jektong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import com.jektong.service.PersonService; import com.jektong.spring.Person; @Configuration @ComponentScan(value = "com.jektong",includeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE,value= {PersonService.class}) },useDefaultFilters = false ) public class PersonConfig { @Bean("person01") public Person person() { return new Person("李四",21); } }
盡管我們將PersonService.java上的注解去掉,使用ASSIGNABLE_TYPE依然會(huì)加載出來(lái)(自行測(cè)試)。
ASPECTJ與REGEX基本不用,不用了解。
以上就是@ComponentScan的具體用法,該興趣的話可以看一下源碼。
到此這篇關(guān)于詳解Spring系列之@ComponentScan自動(dòng)掃描組件的文章就介紹到這了,更多相關(guān)Spring @ComponentScan內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Spring系列之@ComponentScan批量注冊(cè)bean
- SpringBoot默認(rèn)包掃描機(jī)制及@ComponentScan指定掃描路徑詳解
- SpringBoot中@ComponentScan的使用詳解
- Springboot項(xiàng)目實(shí)現(xiàn)將類從@ComponentScan中排除
- 為什么說(shuō)要慎用SpringBoot @ComponentScan
- Spring注解開(kāi)發(fā)@Bean和@ComponentScan使用案例
- springboot @ComponentScan注解原理解析
- Spring @ComponentScan注解掃描組件原理
相關(guān)文章
Java開(kāi)發(fā)者就業(yè)需要掌握的9大專業(yè)技能
這篇文章主要為大家詳細(xì)介紹了java就業(yè)前需要掌握的專業(yè)技能,感興趣的小伙伴們可以參考一下2016-09-09springboot+mybatis+redis 二級(jí)緩存問(wèn)題實(shí)例詳解
Mybatis默認(rèn)沒(méi)有開(kāi)啟二級(jí)緩存,需要在全局配置(mybatis-config.xml)中開(kāi)啟二級(jí)緩存。本文講述的是使用Redis作為緩存,與springboot、mybatis進(jìn)行集成的方法。需要的朋友參考下吧2017-12-12Java關(guān)鍵字synchronized基本使用詳解
這篇文章主要給大家介紹了關(guān)于Java關(guān)鍵字synchronized基本使用的相關(guān)資料,synchronized可以用來(lái)同步靜態(tài)和非靜態(tài)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Spring學(xué)習(xí)筆記之RestTemplate使用小結(jié)
這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之RestTemplate使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Spring @Lookup深入分析實(shí)現(xiàn)原理
這篇文章主要介紹了Spring @Lookup實(shí)現(xiàn)原理,我們知道在spring容器中單獨(dú)的一個(gè)抽象類是不能成為一個(gè)bean的,那么有沒(méi)有辦法呢?這個(gè)時(shí)候我們可以使用Lookup注解2023-01-01SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案
這篇文章主要介紹了SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08RocketMQ?offset確認(rèn)機(jī)制示例詳解
這篇文章主要為大家介紹了RocketMQ?offset確認(rèn)機(jī)制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09java實(shí)現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問(wèn)題示例
這篇文章主要介紹了java實(shí)現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問(wèn)題示例,需要的朋友可以參考下2014-05-05SpringBoot中的RestTemplate使用方法詳解
這篇文章主要介紹了SpringBoot中的RestTemplate使用方法詳解,為了方便使用,這里我封裝成一個(gè)工具類來(lái)靜態(tài)調(diào)用RestTemplate,基于SpringBoot2.4.2版本,需要的朋友可以參考下2024-01-01SpringBoot全局異常處理與定制404頁(yè)面的方法
這篇文章主要介紹了SpringBoot全局異常處理與定制404頁(yè)面的相關(guān)資料,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2007-09-09