当前位置 : 主页 > 编程语言 > c++ >

通过spring框架自身的方法及实现,扫描指定包下的所有类,并通过反射获取类的

来源:互联网 收集:自由互联 发布时间:2021-06-30
扫描指定包下的所有类 package com.common.service;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annot
扫描指定包下的所有类
package com.common.service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.VisitConfine;

import io.swagger.annotations.ApiOperation;

@Service
public class PackageScanService {
	
	@Value("${server.context-path}")
	private String serverContextPath;
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String RESOURCE_PATTERN = "**/%s/**/*.class";
	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	
	public List
 
   scanAllClassFile(String basePackage){
		 String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX  + 
				          String.format(RESOURCE_PATTERN,ClassUtils.convertClassNameToResourcePath(basePackage));
		 List
  
    result = new ArrayList
   
    (); try{ Resource[] resources = this.resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader reader = readerFactory.getMetadataReader(resource); Class
     curClass = Thread.currentThread().getContextClassLoader().loadClass(reader.getClassMetadata().getClassName()); // 获取类上的RequestMapping RequestMapping classRequestMapping = curClass.getAnnotation(RequestMapping.class); String[] classReqPath = new String[]{}; if(null != classRequestMapping){ // 有可能类上没有注解 classReqPath = classRequestMapping.value(); } Method[] methods = curClass.getDeclaredMethods(); for(Method method : methods){ VisitConfine visitConfine = method.getAnnotation(VisitConfine.class); if(null != visitConfine && visitConfine.authed()){ // 只查找需要权限的方法 ApiOperation apiOperation = method.getAnnotation(ApiOperation.class); String methodReqPath[] = method.getAnnotation(RequestMapping.class).value(); // 方法上的RequestMapping for(String str1: classReqPath){ for(String str2 : methodReqPath){ result.add(new com.common.entity.Resource(apiOperation.value(),serverContextPath + "/" + str1 + "/" + str2)); } } } } } } return result; }catch (Exception e) { e.printStackTrace(); logger.error("扫描提取[{}]包路径下,标记了注解[{}]的类出现异常", pattern); return null; } } }
   
  
 
网友评论