转载-通过ApplicationContext 去获取所有的Bean
Spring Boot - 获取所有的Bean信息 阅读目录 前言 通过ApplicationContext 去获取所有的Bean 前言 Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息; 通过ApplicationContext 去获取所有的Bean 通过CommandLineRunner接口,可以实现在Spring Boot完全启动后执行一些代码逻辑,本文将执行的逻辑是打印所有Bean的信息; 1) 通过 ApplicationContext.getBeanDefinitionNames() 方法获取所有Bean的名称; 2) 通过 ApplicationContext.getBean(beanName)获取Bean的详细信息; 具体代码实现如下: package com.howtodoinjava.app.controller; import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.ApplicationContext; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } @Autowired private ApplicationContext appContext; @Override public void run(String... args) throws Exception { String[] beans = appContext.getBeanDefinitionNames(); Arrays.sort(beans); for (String bean : beans) { System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass()); } } } 运行以上程序,控制台将打印如下信息: 2017-03-06 13:22:50 - Tomcat started on port(s): 8080 (http) basicErrorController of Type :: class org.springframework.boot.autoconfigure.web.BasicErrorController beanNameHandlerMapping of Type :: class org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping beanNameViewResolver of Type :: class org.springframework.web.servlet.view.BeanNameViewResolver characterEncodingFilter of Type :: class org.springframework.boot.web.filter.OrderedCharacterEncodingFilter conventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorViewResolver defaultServletHandlerMapping of Type :: class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping defaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolver dispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServlet dispatcherServletRegistration of Type :: class org.springframework.boot.web.servlet.ServletRegistrationBean duplicateServerPropertiesDetector of Type :: class org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration$DuplicateServerPropertiesDetector embeddedServletContainerCustomizerBeanPostProcessor of Type :: class org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor error of Type :: class org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView errorAttributes of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorAttributes ... ... ... @Author 风一样的码农 @HomePageUrl http://www.cnblogs.com/chenpi/ @Copyright 转载请注明出处,谢谢~
不恋尘世浮华,不写红尘纷扰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理