最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring系列之@ComponentScan自動掃描組件

 更新時間:2022年06月14日 11:29:56   作者:爪哇斗羅  
這篇文章主要介紹了Spring @ComponentScan自動掃描組件使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

無注解方式component-scan使用

之前,我們需要掃描工程下一些類上所標注的注解,這些常用注解有:

@Controller,@Service,@Component,@Repository

通過在Spring的配置文件中配置<context:component-scan>掃描對應包下掃描這些注解的方式:

<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使用

建三個類,依次將

@Controller,@Repository,@Service,標注這些類:

圖1

現(xiàn)在通過使用注解@ComponentScan的方式來掃描所在包下面的這些類:之前定義的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);
	}
}

測試,看是否掃描到這些注解所標注的類: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);
	}
}

測試效果:除了Spring要自動加載的配置類以外也顯示了剛才添加的配置類:

圖2

為何會出現(xiàn)PersonConfig,因為@Configuration本 身就是@Component注解的:

圖3

@ComponentScan的掃描規(guī)則

如果需要指定配置類的掃描規(guī)則,@ComponentScan提供對應的掃描方式@Filter進行配置類的過濾:

// 掃描包的時候只規(guī)定掃描一些注解配置類。
Filter[] includeFilters() default {};
// 掃描包的時候可以排除一些注解配置類。 
Filter[] excludeFilters() default {};

Filter其實也是一個注解,相當于@ComponentScan的子注解,可以看圖4:

圖4

Filter對應的過濾規(guī)則如下:

第一種:掃描包的時候只規(guī)定掃描一些注解配置類【includeFilters】。

使用這個includeFilters過濾規(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);
	}
}

這樣就只會掃描用@Controller,標注的配置類交給Spring容器中了:

圖5

第二種:掃描包的時候可以排除一些注解配置類【excludeFilters】。

圖6

@Filter看上圖,有5種不同類型的過濾策略。拿第一種舉例,我們需要過濾使用@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);
	}
}

測試看一下發(fā)現(xiàn)圖2中的personController不會交給Spring容器去管理了:

圖7

上面的圖6展示出5種不同類型的過濾策略,上面介紹了注解類型(FilterType.ANNOTATION),還有四種:

重點看一下CUSTOM自定義掃描策略。

從源碼看,自定義掃描注解類型需要實現(xiàn)TypeFilter接口,下面就寫一個實現(xiàn)類MyFilter.java:在實現(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 {
		// 查看當前類的注解。
		AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
		// 查看當前掃描類的信息
		ClassMetadata classMetadata = metadataReader.getClassMetadata();
		// 獲取當前類資源
		Resource resource = metadataReader.getResource();
		String className = classMetadata.getClassName();
		System.out.println("className===>" + className);
		// 只要類名包含er則注冊Spring容器
		if(className.contains("er")) {
			return true;
		}
		return false;
	}
}

測試:

PersonConfig 中進行掃描:

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”的全部掃描出來,并配置給Spring容器:

ASSIGNABLE_TYPE:按照指定的類型去加載對應配置類:

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依然會加載出來(自行測試)。

ASPECTJ與REGEX基本不用,不用了解。

以上就是@ComponentScan的具體用法,該興趣的話可以看一下源碼。

到此這篇關(guān)于詳解Spring系列之@ComponentScan自動掃描組件的文章就介紹到這了,更多相關(guān)Spring @ComponentScan內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot配置security basic path無效解決方案

    Springboot配置security basic path無效解決方案

    這篇文章主要介紹了Springboot配置security basic path無效解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié)

    SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié)

    本文主要介紹了SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié),分別是InMemoryTokenStore,JdbcTokenStore,JwkTokenStore,RedisTokenStore,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • java發(fā)送短信的實現(xiàn)步驟

    java發(fā)送短信的實現(xiàn)步驟

    下面小編就為大家?guī)硪黄猨ava發(fā)送短信的實現(xiàn)步驟。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Spring Data JPA使用JPQL與原生SQL進行查詢的操作

    Spring Data JPA使用JPQL與原生SQL進行查詢的操作

    這篇文章主要介紹了Spring Data JPA使用JPQL與原生SQL進行查詢的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring Cache的基本使用與實現(xiàn)原理詳解

    Spring Cache的基本使用與實現(xiàn)原理詳解

    緩存是實際工作中非經(jīng)常常使用的一種提高性能的方法, 我們會在很多場景下來使用緩存。下面這篇文章主要給大家介紹了關(guān)于Spring Cache的基本使用與實現(xiàn)原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-05-05
  • SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼

    SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼

    這篇文章主要介紹了SpringBoot實現(xiàn)阿里云短信接口對接的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Mockito mock Kotlin Object類方法報錯解決方法

    Mockito mock Kotlin Object類方法報錯解決方法

    這篇文章主要介紹了Mockito mock Kotlin Object類方法報錯解決方法,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 詳解5種Java中常見限流算法

    詳解5種Java中常見限流算法

    在高并發(fā)系統(tǒng)中,出于系統(tǒng)保護角度考慮,通常會對流量進行限流;不但在工作中要頻繁使用,而且也是面試中的高頻考點。本文就為大家整理了5種Java中常見限流算法,需要的可以參考一下
    2023-04-04
  • java多線程編程之java線程簡介

    java多線程編程之java線程簡介

    線程是程序運行的基本執(zhí)行單元,線程不僅可以共享進程的內(nèi)存,而且還擁有一個屬于自己的內(nèi)存空間,這段內(nèi)存空間也叫做線程棧
    2014-01-01
  • 解析本地方法映射Java層的數(shù)據(jù)類型

    解析本地方法映射Java層的數(shù)據(jù)類型

    這篇文章給大家介紹了本地方法映射Java層的數(shù)據(jù)類型,包括基礎類型映射,引用類型映射等等,對java層數(shù)據(jù)類型映射相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-03-03

最新評論

龙南县| 抚远县| 湘阴县| 革吉县| 济宁市| 邻水| 汽车| 贵州省| 桦甸市| 怀来县| 德江县| 永州市| 南昌市| 威宁| 旺苍县| 肇东市| 扎囊县| 景宁| 益阳市| 鄢陵县| 临沂市| 陆良县| 灯塔市| 渝北区| 湄潭县| 通江县| 洛扎县| 饶平县| 从化市| 绵阳市| 剑河县| 开封市| 襄城县| 法库县| 饶阳县| 乾安县| 达州市| 普定县| 含山县| 花莲市| 绥德县|