前言
这里发一个案例,获取 RestController
注解的所有bean
。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@Component public class InitRestControllerBeanCache implements ApplicationListener<ContextRefreshedEvent> {
@Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().getParent() == null) { Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(RestController.class); for (Object bean : beans.values()) { System.err.println(bean == null ? "null" : bean.getClass().getName()); } } } }
|
本文地址: https://github.com/maxzhao-it/blog/post/380b5f6e/