SpringBoot如何獲取當前項目所有Controller接口的兩種方式

業務場景本身想,只分享技術實現。下面的演示因爲我爲了方便,直接在公司項目中寫的,因此一些敏感信息專有名詞我要註釋,望諒解。

首先咱們都知道,Spring的IOC機制,全部的接口和service都存在一個map容器,經過BeanFactory和ApplicationContext能夠拿。那麼咱們能夠從這個開刀.java

方案一

(1)想一想有什麼方法或者接口是可以獲取到ApplicationContext的,答案固然是有,Aware接口嘛,找到一個ApplicationContextAware,理論上就能獲取到ApplicationContext容器自己。

關於Aware接口的詳細描述:SpringBoot中的Aware接口
在這裏插入圖片描述
在這裏插入圖片描述

app

(2)ApplicationContext拿到了,剩下的其實就是從裏面拿到接口而已,這裏貪圖方便,就直接重寫啓動以後的run方法裏面作了。

CommandLineRunner:經過實現這個接口,重寫run方法,能夠在啓動類完成後執行要作的事情,若是多個方法都繼承了CommandLineRunner接口,多個run方法都要執行,同時要有前後順序,加@Order(value = )配權重就能夠了。ide

@Component
@Order(value = 2)
public class A implements CommandLineRunner{ 
@Override
public void run(String... strings) throws Exception { 
   
    }
}

@Component
@Order(value = 1)
public class B implements CommandLineRunner { 
@Override
public void run(String... strings) throws Exception { 
   
    }
}

這樣,B必定會啓動成功後先於A先執行。
扯遠了,說回來,爲方便直接寫:
在這裏插入圖片描述

post

@Override
    public void run(String... args) throws Exception { 
        //獲取使用RestController註解標註的的全部controller類
        Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(RestController.class);
        //遍歷每一個controller層
        for (Map.Entry<String, Object> entry : controllers.entrySet()) { 
            Object value = entry.getValue();
            System.out.println("拿到controller:"+entry.getKey()+",拿到value:"+value);
            Class<?> aClass = AopUtils.getTargetClass(value);
            System.out.println("拿到Class:"+aClass);
            }
            }
啓動,直接看輸出結果:

在這裏插入圖片描述
能夠看到找到了GoodController 和AnalysisController,可是卻沒有找到我特地舉例的一個Good2Controller
在這裏插入圖片描述

this

緣由就是Good2我沒有使用RestController註解,而是使用了 @Controller + @ResponseBody,這是個小坑,不要覺得RestController複合註解裏面包含了@Controller 就會理所固然被發現,結果是並不會。由於這裏是根據接口類型找的,它不關心你接口裏麪包着什麼,只要不是RestController.class,統一發現不了。

在這裏插入圖片描述
在這裏插入圖片描述

(3)咱們已經成功拿到了所有controller,可是咱們都知道接口指的是controller下面的一個個public方法,因而繼續往下找。在上面的代碼繼續加入這一行。

在這裏插入圖片描述
運行結果:
在這裏插入圖片描述
成功打印出來~


url

(4)若是咱們不想發現所有的方法,而是有選擇性的發現Get,Post,Put之類的方法,也很簡單
// 首先拿到全部的方法
	List<Method> declaredMethods = Arrays.asList(aClass.getDeclaredMethods());
            for (int i = 0; i < declaredMethods.size() ; i++) { 
                // 下面開始根據註解類型進行輸出統計
                GetMapping getMapping = declaredMethods.get(i).getAnnotation(GetMapping.class);
                PostMapping postMapping = declaredMethods.get(i).getDeclaredAnnotation(PostMapping.class);
                System.out.println("Get相關的:"+JSON.toJSONString(getMapping));
                System.out.println("Post相關的:"+JSON.toJSONString(postMapping));
            }
成功~!

在這裏插入圖片描述

(5)整體代碼提供,要作統計或者其餘一些什麼接口的話,就本身用Map,SET 實現一下:
public class EarlyWarningApplication implements ApplicationContextAware ,CommandLineRunner { 

    // 定義一個私有的方便本class中調用
    private ApplicationContext applicationContext;

    // 經過重寫ApplicationContextAware感知接口,將ApplicationContext賦值給當前的私有context容器
    @Override
    public void setApplicationContext(ApplicationContext arg0) { 
        this.applicationContext = arg0;
    }

    public static void main(String[] args) { 
        SpringApplication application = new SpringApplication(EarlyWarningApplication.class);
        application.run(args);
    }


    @Override
    public void run(String... args) throws Exception { 
        
        Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(RestController.class);
       
        for (Map.Entry<String, Object> entry : controllers.entrySet()) { 
            Object value = entry.getValue();
            System.out.println("拿到controller:"+entry.getKey()+",拿到value:"+value);
            Class<?> aClass = AopUtils.getTargetClass(value);
            System.out.println("拿到Class:"+aClass);
            RequestMapping annotation = aClass.getAnnotation(RequestMapping.class);
            RequestMapping declaredAnnotation = aClass.getDeclaredAnnotation(RequestMapping.class);
           
            List<Method> methods = Arrays.asList(aClass.getMethods());
            System.out.println("Public Methods:" + methods);
            List<Method> declaredMethods = Arrays.asList(aClass.getDeclaredMethods());
            for (int i = 0; i < declaredMethods.size() ; i++) { 
                GetMapping getMapping = declaredMethods.get(i).getAnnotation(GetMapping.class);
                PostMapping postMapping = declaredMethods.get(i).getDeclaredAnnotation(PostMapping.class);
                System.out.println("Get相關的:"+JSON.toJSONString(getMapping));
                System.out.println("Post相關的:"+JSON.toJSONString(postMapping));
            }
        }
    }
}

方案二

(1)隆重介紹WebApplicationContext 全局接口,屬於ApplicationContext的一個兒子,它的做用不少,可是今天!在這裏!它只用來獲取每一個Controller裏面的所有public接口~

在這裏插入圖片描述

(2)直接上代碼,直接寫在某個Service或者Controller都行,寫在哪都行:
@Autowired
    WebApplicationContext applicationContext;

    @GetMapping("/getParam")
    public String getParam(){ 

        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 拿到Handler適配器中的所有方法
        Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : methodMap.keySet()){ 
           
            Set<String> urlSet = info.getPatternsCondition().getPatterns();
            // 獲取所有請求方式
            Set<RequestMethod> Methods = info.getMethodsCondition().getMethods();
            System.out.println(Methods.toString());
            for (String url : urlSet){ 
                // 加上本身的域名和端口號,就能夠直接調用
                urlList.add("http://localhost:XXXX"+url);
            }
        }
        return urlList.toString();
    }
看下結果:

在這裏插入圖片描述

在這裏插入圖片描述

成功~
相關文章
相關標籤/搜索