@ComponentScan主要就是定義掃描的路徑從中找出標識了需要裝配的類自動裝配到spring的bean容器中。在配置類上添加 @ComponentScan 註解。該註解默認會掃描該類所在的包下所有的配置類,相當於之前的 <context:component-scan>。@ComponentScan註解默認裝配標識了@Controller,@Service,@Repository,@Component註解的類到spring容器中。
例子:
在xml文件配置的方式,我們可以這樣來進行配置:
在配置類裏面寫包掃描:
@Configuration
@ComponentScan(value="com.hlkj")
public class SpringConfig {
@Bean
public Person person() {
return new Person("lisi",20);
}
}
我們創建PersonController、PersonService、PersonDao這幾個類,分別添加了@Controller、@Service、@Repository註解:
@Controller
public class PersonController {
}
@Service
public class PersonService {
}
@Repository
public class PersonDao {
}
單元測試:
@Test
public void test01() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
測試結果:
從上面的測試結果我們可以發現主配置類 SpringConfig 也是IOC容器裏面的組件,也被納入了IOC容器的管理;我們從@Configuration這個註解點進去就可以發現這個註解上也標註了@Component的這個註解,也納入到IOC容器中作爲一個組件。
@ComponentScan詳細配置:
basePackages與value: 用於指定包的路徑,進行掃描
basePackageClasses: 用於指定某個類的包的路徑進行掃描
nameGenerator: bean的名稱的生成器
useDefaultFilters: 是否開啓對@Component,@Repository,@Service,@Controller的類進行檢測
includeFilters: 包含的過濾條件
FilterType.ANNOTATION:按照註解過濾
FilterType.ASSIGNABLE_TYPE:按照給定的類型
FilterType.ASPECTJ:使用ASPECTJ表達式
FilterType.REGEX:正則
FilterType.CUSTOM:自定義規則
excludeFilters: 排除的過濾條件,用法和includeFilters一樣
下面是includeFilters和excludeFilters的例子:
includeFilters和excludeFilters可以指定要排除哪些包或者是隻包含哪些包來進行管理:裏面傳是一個Filter[]數組。
配置excludeFilters:
@Configuration
@ComponentScan(value = "com.hlkj", excludeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class, Service.class})
})
public class SpringConfig {
@Bean
public Person person() {
return new Person("lisi",20);
}
}
@Test
public void test02() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
測試結果如下:這個時候,personService、personController這兩個組件就已經被排除掉了,不再被IOC容器給管理。
配置includeFilters:
@Configuration
@ComponentScan(value = "com.hlkj", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
public class SpringConfig {
@Bean
public Person person() {
return new Person("lisi",20);
}
}
@Test
public void test03() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
測試結果如下:只有一個bookController被納入到IOC容器進行管理。
我們還可以用 @ComponentScans來定義多個掃描規則:裏面是@ComponentScan規則的數組
注:@ComponentScan的常用方式
-
自定掃描路徑下邊帶有@Controller,@Service,@Repository,@Component註解加入spring容器
-
通過includeFilters加入掃描路徑下沒有以上註解的類加入spring容器
-
通過excludeFilters過濾出不用加入spring容器的類
-
自定義增加了@Component註解的註解方式
-
@ComponentScan這個註解是可以重複定義的:來指定不同的掃描策略