在使用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协助调试的更多相关文章

  1. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  2. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

  3. [十]SpringBoot 之 普通类获取Spring容器中的bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  4. Spring Boot中普通类获取Spring容器中的Bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...

  5. 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

    ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述:   即是说,当一个类实现了这个接口之 ...

  6. SpringBoot之普通类获取Spring容器中的bean

    package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...

  7. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  8. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  9. java web中如何获取spring容器中定义的bean----WebApplicationContext的使用

    本文简单编写一个servlet来获取spring容器中管理的<bean  id="dateBean" class="java.util.Date" sin ...

随机推荐

  1. Java DynamoDB 增加、删除、修改、查询

    准备jar包 <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sd ...

  2. 使用go语言实现简单的反向代理工具激活IntelliJ和PyCharm,持续更新

    最近Jetbrians系列IDE更新至2017.3版本,激活检测机制也变成了动态封禁域名,导致大部分域名激活被屏蔽了,所以找了下资料,根据ilanyu的代码,改了下地址,实现了本地反向代理激活服务器. ...

  3. CSDN 夏令营程序 试题分析 (2)

    题目:若须要在O(nlogn)(以2为底)的时间内完毕对数组的排序.且要求排序是稳定的,则可选择的排序方法是: A.高速排序       B.堆排序            C.归并排序  D.直接插入 ...

  4. storm ——Understanding the Parallelism of a Storm Topology

    http://www.michael-noll.com/blog/2012/10/16/understanding-the-parallelism-of-a-storm-topology/ 这篇文章好 ...

  5. linux shell学习一点点

    问题,从shell command 交互式地由用户输入密码,但是输入的过程中不会显示用户输入的密码,起到类似于于html中input type=password的作用. #!/bin/bash rea ...

  6. Dell PowerEdgeServerT110II USB Boot更新

    可引导USB设备更新Dell PowerEdge服务器 当显示Boot Options(“启动选项”)时,选择option 1(选项 1)以开始固件更新. 现在正在加载的Linux发行版本 然后固件更 ...

  7. Apache2 Web 服务器

    Apache2 Web 服务器的安装 sudo apt install apache2 -y 当安装Apache2包之后,Apache2相关的服务是启用的,并在重启后自动运行.在某些情况下如果你的Ap ...

  8. C#.NET 如何打开高版本的sln文件

    我用VS2008去打开2010版本的解决方案,提示如下   其实我可以直接打开这个csproj文件并运行   关闭之后就会提示是否创建一个新的 sln文件

  9. Android之——多线程下载演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...

  10. Python输入输出及其他

    print用法 print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,而且保证我们在下面看到最新的输出结果.回车 \r 本义是光标重 ...