spring获取加载的所有bean

原文地址 : https://blog.csdn.net/long243416336/article/details/80590032
public class SpringContextHolder implements ApplicationContextAware {

private static ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
    SpringContextHolder.applicationContext = applicationContext;
}

public static ApplicationContext getApplicationContext() {
    return applicationContext;
}

public static void printAllBeans() {
    String[] beans = SpringContextHolder.getApplicationContext()
            .getBeanDefinitionNames();
    for (String beanName : beans) {
        Class<?> beanType = SpringContextHolder.getApplicationContext()
                .getType(beanName);
        System.out.println("BeanName:" + beanName);
        System.out.println("Bean的类型:" + beanType);
        System.out.println("Bean所在的包:" + beanType.getPackage());
        System.out.println("Bean:" + SpringContextHolder.getApplicationContext().getBean(
                beanName));
    }
}

}

猜你喜欢

转载自blog.csdn.net/mengxiangxingdong/article/details/82664114