获取Spring容器中的Bean协助调试
在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试。
第一步:编写自定义监听器
/**
* 监听servletContext域中属性发生变化的监听器
* @author Mr.Song
*/
public class ServletContextAttributeListenerImpl
implements ServletContextAttributeListener{
/**
* 存入应用域中时
* @param scab
*/
@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
//1.取出ServletContext对象
ServletContext context = scab.getServletContext();
//2.取出spring的ioc容器(查看源码得知其key)
WebApplicationContext wac = (WebApplicationContext)context.getAttribute
(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//3.取出所有的bean名称
String[] names = wac.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {}
@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {}
}
第二步:在web.xml中配置监听器
<listener>
<listener-class>cn.dintalk.web.listeners.ServletContextAttributeListenerImpl</listener-class>
</listener>
如此,我们便可观察Spring容器中Bean实例创建的情况了。
关注微信公众号,随时随地学习

获取Spring容器中的Bean协助调试的更多相关文章
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
- [十]SpringBoot 之 普通类获取Spring容器中的bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
- Spring Boot中普通类获取Spring容器中的Bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述: 即是说,当一个类实现了这个接口之 ...
- SpringBoot之普通类获取Spring容器中的bean
package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
- java web中如何获取spring容器中定义的bean----WebApplicationContext的使用
本文简单编写一个servlet来获取spring容器中管理的<bean id="dateBean" class="java.util.Date" sin ...
随机推荐
- [bzoj3252]攻略_dfs序_线段树_贪心
攻略 bzoj-3252 题目大意:给定一棵n个节点的有根树,点有点权.让你选出至多k个节点,使得他们到根的链的并最大. 注释:$1\le n\le 2\cdot 10^5$,$1\le val_i\ ...
- HDU——1068 Girls and Boys
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- RabbitMQ环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/zyz511919766/article/details/41896823 http://blog.chinaunix.net/uid- ...
- Django学习系列之路由系统
一.基于App的路由 作用: 根据App对路由规则进行分类,在全局urls.py包含某个项目的urls.py 示例 定义全局urls.py(全局路由系统) #导入include from django ...
- nlssort排序
ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码进行排序的. 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NL ...
- AutoreleasePool 分析
前言 AutoreleasePool自己主动释放池,对于自己主动释放对象的作用怎样? 释放池中的自己主动释放对象什么时候会被释放? MRC环境下 场景1 NSString *string_var_ = ...
- Raphael.js API 之Element.remove(),Element.removeData(),paper.text(),Element.node(),Element.onDragOver
/*API-38*/ Element.remove() 删除某个元素对象,无返回值 /*API-39*/ Element.removeData([key]); 删除某个key的value值.假设没有特 ...
- opencv对图像进行边缘及角点检測
opencv对图像进行边缘及角点检測 先看结果: 代码: // ConsoleApplication1_812.cpp : Defines the entry point for the consol ...
- Python 不同对象比較大小
万恶的源泉: Fireboo的疑问(当然 lambda 本身写的就有问题): >>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, ...
- web container和spring container之间的关系
1 tomcat是一个web container,它提供java web应用的运行环境,因为它实现了好多java ee规范,比如java Servlet.JSP等,也就是说,没有它,java web应 ...