「java開発」spring-coreパッケージを利用してスキャンする方法
jarファイル:spriong-core.jar
ファイル名:PackaheScanner_Startnews24.java
javaコード:
package com.vstartek.dm.util;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
public class PackageScaner_Startnews24 {
static String CLASSPATH_ALL_URL_PREFIX = “classpath*:";
static final String DEFAULT_RESOURCE_PATTERN = “**/*.class";
private String resourcePattern = DEFAULT_RESOURCE_PATTERN;
public Set doScaner(String basePackage) throws IOException{
Set candidates = new LinkedHashSet();
String packageSearchPath = PackageScaner.CLASSPATH_ALL_URL_PREFIX +basePackage.replace('.’, '/’) + “/" + this.resourcePattern;
PathMatchingResourcePatternResolver pmr = new PathMatchingResourcePatternResolver();
Resource[] rs = pmr.getResources(packageSearchPath);
for(Resource r:rs){
CachingMetadataReaderFactory ss= new CachingMetadataReaderFactory();
MetadataReader ma = ss.getMetadataReader(r);
candidates.add(ma);
}
return candidates;
}
}