import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@Component
@Slf4j
public class CommandLiner2 implements ApplicationContextAware,CommandLineRunner {
private ApplicationContext applicationContext;
@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);
log.info("所有方法:"+ JSONUtil.toJsonStr(aClass.getDeclaredMethods()));
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
}
Spring Boot获取到所有controller及里面的方法
最新推荐文章于 2024-11-13 23:26:52 发布