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

监听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;
	}

}

相关文章: