package com.ygtx;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Component
public class PrintAllControllers implements CommandLineRunner {

    private final ApplicationContext applicationContext;

    public PrintAllControllers(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("====== 已注册的 Controller 映射 ======");
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        mapping.getHandlerMethods().forEach((key, value) -> {
            System.out.println(key + " => " + value.getBeanType().getName());
        });
        System.out.println("=====================================");
    }
}
