监听spring容器个数

监听spring容器个数
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说监听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;
	}

}

本文来源GroovRain,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/25382

发表评论