监听spring容器个数

版权声明:版权所有,禁止转载,违者必究。喜欢的朋友可以关注博主以及点赞评论喔,未来将会持续更新javaweb相关的内容。 您的支持是我更新最大的动力~ https://blog.csdn.net/hxfghgh/article/details/83005437

查看spring扫描的容器数量,用于日常的排解错误!

在这里插入图片描述

package com.hejie.component;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class SpringContextListener implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {

	private static ApplicationContext applicationContext;
	
	public void onApplicationEvent(ContextRefreshedEvent event) {
		// TODO Auto-generated method stub
		
		String[] beans = this.applicationContext.getBeanDefinitionNames();
		
		for (String string : beans) {
			Class<?> type = this.applicationContext.getType(string);
			System.out.println("name: " + string);
			System.out.println("type: "+ type);
			System.out.println("---------------");
		}
	
		
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		 this.applicationContext = applicationContext;
	}

}

猜你喜欢

转载自blog.csdn.net/hxfghgh/article/details/83005437