1. @ComponentScan
註解
在配置類中只要標註了@ComponentScan
註解,Spring就可以自動掃描Value對應的包了
1.1. 配置類
package com.spring.config;
import com.spring.beans.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = "com.spring")
public class MainConfig {
@Bean
public Person person() {
return new Person("fj",26);
}
}
以上掃描com.spring包中的所有組件
1.2 單元測試類
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class IOCTest_1 {
@SuppressWarnings("resource")
@Test
public void test01() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
// 獲取容器中的所有組件
String[] names = applicationContext.getBeanDefinitionNames();
for(String name: names)
System.out.println(name);
}
}
輸出的結果
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
bookController
bookService
bookDao
person
前面五個都是Spring自己的組件,其中mainConfig
類是因爲增加了@Configuration
註解,bookController
類是因爲增加了@Controller
註解,bookService
類是因爲增加了@Servide
註解,bookDao
類是因爲增加了@Repository
註解,pereson
類是因爲在mainConfig
類的person方法中增加了@Bean
註解
1.3 excludeFilters
排除
package com.spring.config;
import com.spring.beans.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(value = "com.spring", excludeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class})
})
public class MainConfig {
@Bean
public Person person() {
return new Person("fj",26);
}
}
根據註解類型進行排除
1.4 includeFilters
僅包含
package com.spring.config;
import com.spring.beans.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(value = "com.spring", includeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
public class MainConfig {
@Bean
public Person person() {
return new Person("fj",26);
}
}
使用僅包含includeFilters
需要禁用默認的過濾規則,即將useDefaultFilters
設置爲false
1.5 FilterType.CUSTOM
自定義過濾規則
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;
public class MyTypeFilter implements TypeFilter {
@Override
// MetadataReader: 讀取到的當前正在掃描的類的信息
// MetadataReaderFactory: 可以獲取到其他任何類的信息
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("dao"))
return true;
return false;
}
}
總結
excludeFilers = Filter[]
, 指定掃描的時候按照什麼規則排除哪些組件includeFilers = Filter[]
, 指定掃描的時候按照什麼規則包含哪些組件FilterType.ANNOTATION
, 按照註解FilterType.ASSIGNABLE_TYPE
,按照給定的類型FilterType.ASPECTJ
,使用ASPECTJ表達式FilterType.REGEX
,使用正則規則FilterType.CUSTOM
,使用自定義規則@ComponentScan
註解還可以疊加使用