spring获取加载的所有bean

原文链接: https://blog.csdn.net/long243416336/article/details/80590032

原文地址 : 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));
		}
	}
 
}

你可能感兴趣的:(【spring】)