Spring註解驅動 自定義TypeFilter
阿新 • • 發佈:2019-01-29
自定義過濾規則需要實現TypeFilter介面
步驟一:自定義類實現TypeFilter介面
package com.gcxzflgl.filter; 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 { /** * metadataReader:讀取到的當前正在掃描的類的資訊 * metadataReaderFactory:可以獲取到其他任何類資訊的 */ public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { // TODO Auto-generated method stub //獲取當前類註解的資訊 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //獲取當前正在掃描的類的類資訊 ClassMetadata classMetadata = metadataReader.getClassMetadata(); //獲取當前類資源(類的路徑) Resource resource = metadataReader.getResource(); String className = classMetadata.getClassName(); System.out.println("--->"+className); if(className.contains("ll")){ return true; } return false; } }
步驟二:新增自定義配置
@ComponentScans(value={
@ComponentScan(value="com.gcxzflgl",includeFilters = {
@ComponentScan.Filter(type=FilterType.CUSTOM,classes= MyTypeFilter.class)}
,useDefaultFilters = false)
})
步驟三:主類執行,主要是帶有 ll的就會被掃描到