自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring系列之@ComponentScan注解用法介紹

開發(fā) 架構(gòu)
今天給大家分享Spring中@ComponentScan注解的用法,希望對大家能有所幫助!

今天給大家分享Spring中@ComponentScan注解的用法,希望對大家能有所幫助!

1、@ComponentScan注解的作用

@ComponentScan注解一般和@Configuration注解一起使用,主要的作用就是定義包掃描的規(guī)則,然后根據(jù)定義的規(guī)則找出哪些需類需要自動裝配到spring的bean容器中,然后交由spring進行統(tǒng)一管理。說明:針對標注了@Controller、@Service、@Repository、@Component 的類都可以別spring掃描到。

2、@ComponentScan注解屬性介紹

2.1 value

指定要掃描的包路徑

2.2 excludeFilters(排除規(guī)則)

excludeFilters=Filter[] 指定包掃描的時候根據(jù)規(guī)則指定要排除的組件

2.3 includeFilters(包含規(guī)則)

includeFilters =Filter[] 指定包掃描的時候根據(jù)規(guī)則指定要包含的組件.注意:要設(shè)置useDefaultFilters = false(系統(tǒng)默認為true,需要手動設(shè)置) includeFilters包含過濾規(guī)則才會生效。

2.4 FilterType屬性

FilterType.ANNOTATION:按照注解過濾FilterType.ASSIGNABLE_TYPE:按照給定的類型,指定具體的類,子類也會被掃描到FilterType.ASPECTJ:使用ASPECTJ表達式FilterType.REGEX:正則FilterType.CUSTOM:自定義規(guī)則useDefaultFilters: 配置是否開啟可以對加@Component,@Repository,@Service,@Controller注解的類進行檢測, 針對Java8 語法可以指定多個@ComponentScan,Java8以下可以用 @ComponentScans() 配置多個規(guī)則

3、示例

3.1 各種過濾過濾規(guī)則示例



// includeFilters 用法 包含Animal.class類可以被掃描到,包括其子類
@ComponentScan(value = "com.spring"
includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {Animal.class}
)}
)

// excludeFilters 用法 排除包含@Controller注解的類
@ComponentScan(value = "com.spring"
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class}
),

})

// ComponentScans用法
@ComponentScans(
value = {
@ComponentScan(value = "com.spring"
, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class}
)
}, useDefaultFilters = false) ,
@ComponentScan(value = "com.spring"
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = { Repository.class}
)
})
}
)*/

// @ComponentScan
// 針對Java8 語法可以指定多個@ComponentScan,Java8以下可以用 //@ComponentScans() 配置多個規(guī)則
@ComponentScan(value = "com.spring"
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class, Controller.class}
),

}, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Controller.class, Controller.class}
),

})

3.2 自定義過濾規(guī)則 需要新建 TestTypeFilter.java



package com.spring.config;

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;

import java.io.IOException;

/**
* metadataReader 讀取到當前正在掃描的類信息
* metadataReaderFactory 可以獲取到其他任何類的信息
*/
public class TestTypeFilter implements TypeFilter {
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);
if (className.contains("controller")) {
return true;
} else {
return false;
}
}
}

3.3 新建測試類 TestComponentScan.java

package com.spring.test;

import com.spring.config.TestComponentScanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestComponentScan {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestComponentScanConfig.class);
String[] names = annotationContext.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}

具體的運行效果可以查看控制臺輸出結(jié)果,是否和預(yù)期的一樣,具體有不清楚的歡迎溝通交流。

責任編輯:姜華 來源: IT技術(shù)分享社區(qū)
相關(guān)推薦

2022-02-19 07:41:36

Bean注解項目

2022-03-03 07:34:31

注解容器作用域

2022-12-19 08:14:30

注解開發(fā)配置

2021-09-16 10:05:09

鴻蒙HarmonyOS應(yīng)用

2023-08-04 08:25:03

客戶配置Spring

2021-07-09 07:52:34

SpringContextEventListen

2024-02-23 10:33:34

SpringBean容器

2016-09-21 14:17:11

JUnit架構(gòu)API

2016-09-23 10:34:32

JUnitGradleAgile

2022-06-09 07:27:14

JavaSpring容器

2022-05-30 11:17:44

Spring容器配置

2024-01-03 07:57:11

高級參數(shù)PowerShellVerbose 參數(shù)

2023-09-21 07:06:17

PSDriveProvider

2024-10-17 09:45:03

2016-09-23 10:20:22

JUnit擴展模型Extension

2021-05-06 18:17:52

SpringAOP理解

2021-05-07 21:32:51

SpringIOC分析

2017-04-24 12:07:44

Spark大數(shù)據(jù)并行計算

2020-11-05 11:30:46

PythonNumPy數(shù)組

2023-08-26 11:36:31

Java框架Spring
點贊
收藏

51CTO技術(shù)棧公眾號