gui組件是什么,4、組件注冊-自定義TypeFilter指定過濾規則

 2023-12-25 阅读 26 评论 0

摘要:4、組件注冊-自定義TypeFilter指定過濾規則 4.1 FilterType.ANNOTATION 按照注解方式 4.2 FilterType.ASSIGNABLE_TYPE 按照給定的類型 @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}) 4.3 FilterType.ASPECTJ 按照AS

4、組件注冊-自定義TypeFilter指定過濾規則

4.1 FilterType.ANNOTATION 按照注解方式

4.2 FilterType.ASSIGNABLE_TYPE 按照給定的類型

@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class})

4.3 FilterType.ASPECTJ 按照ASPECTJ表達式

4.4 FilterType.REGEX 按照正則表達

4.5 FilterType.CUSTOM 按照自定義規則

@ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,includeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
//                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}),@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})}
)       // 指定只掃描
  • 創建MyTypeFilter類,需要實現TypeFilter 接口,重寫match()方法
package com.hw.springannotation.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;/*** @Description TODO* @Author hw* @Date 2018/11/27 18:49* @Version 1.0*/
public class MyTypeFilter implements TypeFilter {/*** Determine whether this filter matches for the class described by* the given metadata.** @param metadataReader        the metadata reader for the target class    讀取到得當前正在掃描的類的信息* @param metadataReaderFactory a factory for obtaining metadata readers    一個可以探索其他類信息的工廠類*                              for other classes (such as superclasses and interfaces)* @return whether this filter matches* @throws IOException in case of I/O failure when reading metadata*/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("er")) {return true;}return false;}
}

1221855-20181127190931210-1067806230.png

轉載于:https://www.cnblogs.com/Grand-Jon/p/10018643.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/194917.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息