說明
我們在使用spring配置文件或者是註解的時有時會看到以下內容:
- 配置文件:
<!--包掃描 掃描後注入@Controller @Service @Component @Repository到容器中-->
<context:component-scan base-package="Spring" >
<!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>-->
<context:exclude-filter type="custom" expression="Spring.config.MyComponentFilter"></context:exclude-filter>
</context:component-scan>
<bean name="myFilter" class="Spring.config.MyComponentFilter"></bean>
- 註解
@ComponentScans(value = {
@ComponentScan(basePackages = {"Spring"},excludeFilters = {
// @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
// @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = PsersonDao.class)+
@ComponentScan.Filter(type = FilterType.CUSTOM,classes = MyComponentFilter.class)
})
})
那麼這裏的FilterType到底是什麼意思呢?下面爲大家說明下。
FilterType
在spring中FilterType包括以下幾類
public enum FilterType {
ANNOTATION, //按照註解過濾
ASSIGNABLE_TYPE, //按照類型過濾
ASPECTJ,//按照ASPECTJ表達式過濾
REGEX,//按照正則表達式過濾
CUSTOM;//按照自定義的過濾規則過濾
private FilterType() {
}
}
- 按照註解過濾:
就是看要注入到容器的類上有哪些註解類似@Controller @Service 這些。 - 按照類型過濾:
可以指定需要過濾的組件的類型,類似於xxx.class - 按照ASPECTJ表達式過濾:
就是用用ASPECTJ定義過濾規則 - 按照正則表達式過濾:
使用正則表達式定義過濾規則 - 自定義的過濾規則過濾:
spring提供了可自定的過濾規則的方式,按照你自己定義的規則進行過濾。
接下來通過配置和註解兩種方式來爲大家說明:
配置文件方式實現
- 如果不使用自定義的過濾方式,那麼只需要在spring的配置文件中按照以下方式配置
<!--包掃描 掃描後注入@Controller @Service @Component @Repository到容器中-->
<context:component-scan base-package="Spring" >
<!-- 以下方式就是按照註解配置的方式 需要過濾的是Controller註解,除了自定義之外的其他方式也可以參考一下方式定義-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
- 如果要使用自定義的過濾規則,這裏就需要大家注意下,需要自己手動寫一個過濾規則的類,並且需要實現org.springframework.core.type.filter.TypeFilter。如下:
/**
* @Description
* @auther Eleven
* @create 2020-04-11 15:51
**/
public class MyComponentFilter implements TypeFilter {
/**
* @param metadataReader 讀取當前正在掃描的類的信息
* @param metadataReaderFactory 可以讀取到其他任何類的信息
* @return
* @throws IOException
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
String className = metadataReader.getClassMetadata().getClassName();
System.out.println("當前正在掃描的類名:"+className);
//這裏的規則是如果類名中包含Service就返回true
return className.contains("Service");
}
}
接下來需要在配置文件中配置如下:
<context:component-scan base-package="Spring" >
<context:exclude-filter type="custom" expression="Spring.config.MyComponentFilter"></context:exclude-filter>
</context:component-scan>
<bean name="myFilter" class="Spring.config.MyComponentFilter"></bean>
- 測試方法
可以建立一些類,放到被掃描的包下,然後看看掃描到的類的內容
public static void main( String[] args )
{
//讀取配置文件 spring配置文件這裏命名爲bean.xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//獲得IOC容器中的加載的類並且輸出
String[] arr = applicationContext.getBeanDefinitionNames();
for (String i:arr){
System.out.println(i);
}
System.out.println( p );
}
註解方式
- 如果不使用自定義的過濾方式, 在配置類中寫如下註解即可
/**
* @Description
* @auther Eleven
* @create 2020-04-11 14:58
**/
//標記這是一個配置文件
@Configuration
@ComponentScans(value = {
@ComponentScan(basePackages = {"Spring"}, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = PsersonDao.class)
})
})
public class MyConfig {
}
- 如果要使用自定義規則過濾也要參考配置文件實現中的第二步寫一個自定的過濾規則類這裏不再贅述。
- 測試方法,首先要在自定義的配置類上寫入以下註解
/**
* @Description
* @auther Eleven
* @create 2020-04-11 14:58
**/
//標記這是一個配置文件
@Configuration
@ComponentScans(value = {
@ComponentScan(basePackages = {"Spring"}, excludeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyComponentFilter.class)
})
})
public class MyConfig {
}
public class App
{
public static void main( String[] args )
{
//讀取註解
ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(MyConfig.class);
//打印出IOC容器中包含的組件
String[] arr = applicationContext1.getBeanDefinitionNames();
for (String i:arr){
System.out.println(i);
}
}
}