在开发时有时候我们需要通过指定的条件找到class文件所在的位置然后进行调用。我们来看下如何找到相应的class文件,这里用到了spring提供的工具类。
package com.cjzj.util;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
public class SearchClassUtil {
public static void main(String[] args) throws IOException {
String path = "classpath*:com/cjzj/model/**/*.class";
String[] configLocations = StringUtils.tokenizeToStringArray(path,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
for (String configLocation : configLocations) {
Resource[] resources = resourceLoader.getResources(configLocation);
// 迭代资源文件