打印spring中所有的注入的bean

本文介绍了如何在Spring Boot应用中通过自动注入的方式获取ApplicationContext,并展示了如何简单地打印ApplicationContext的内容,帮助开发者更好地理解和调试应用的依赖注入情况。

1、首先通过自动注入方式将ApplicationContext注入进去,如:
在这里插入图片描述
2、然后找个地方打印即可,如:
在这里插入图片描述

<think> 我们使用Spring的ApplicationContext来获取所有bean的定义名称,然后遍历这些名称并打印每个bean名。 以下是完整的代码示例: </think> 在 Spring 应用程序中打印所有注册的 bean 名称及其名,可以通过 `ApplicationContext` 获取所有 bean 定义信息。以下是实现代码: ```java import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public CommandLineRunner printBeans(ApplicationContext context) { return args -> { System.out.println("Spring 容器中的所有 bean:"); System.out.println("----------------------------------------"); // 获取所有 bean 定义名称 String[] beanNames = context.getBeanDefinitionNames(); // 按字母顺序排序 Arrays.sort(beanNames); // 打印每个 bean 的名称和型 for (String beanName : beanNames) { Class<?> beanType = context.getType(beanName); System.out.printf("Bean 名称: %-40s | 型: %s%n", beanName, beanType != null ? beanType.getName() : "未知型"); } System.out.println("----------------------------------------"); System.out.println("总计: " + beanNames.length + " 个 bean"); }; } } ``` ### 关键点说明: 1. **使用 CommandLineRunner** - 在 Spring Boot 启动后自动执行打印逻辑 - 通过依赖注入获取 `ApplicationContext` 2. **获取所有 bean 名称** `String[] beanNames = context.getBeanDefinitionNames()` 返回所有已注册 bean 的名称数组 3. **获取 bean 型** `context.getType(beanName)` 返回 bean 的 Class 对象 4. **格式化输出** - 按字母顺序排序便于查看 - 使用格式化输出对齐显示 - 显示未知型(代理可能返回 null) ### 输出示例: ``` Spring 容器中的所有 bean: ---------------------------------------- Bean 名称: demoApplication | 型: com.example.DemoApplication Bean 名称: org.springframework.boot.autoconfigure.AutoConfigurationPackages | 型: org.springframework.boot.autoconfigure.AutoConfigurationPackages Bean 名称: org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration | 型: org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration Bean 名称: propertySourcesPlaceholderConfigurer | 型: org.springframework.context.support.PropertySourcesPlaceholderConfigurer Bean 名称: requestMappingHandlerAdapter | 型: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter ... ---------------------------------------- 总计: 78 个 bean ``` ### 其他实现方式: **1. 在启动中直接打印:** ```java public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args); Arrays.stream(ctx.getBeanDefinitionNames()) .sorted() .forEach(name -> System.out.println(name + " -> " + ctx.getBean(name).getClass().getName())); } ``` **2. 使用 ApplicationContextAware 接口:** ```java @Component public class BeanPrinter implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext context) { String[] beans = context.getBeanDefinitionNames(); for (String bean : beans) { System.out.println(bean + " : " + context.getBean(bean).getClass().getName()); } } } ``` **注意事项:** 1. 启动时某些 bean 可能尚未完全初始化 2. 代理(如 AOP 生成的)会显示为代理型 3. 包含所有 Spring Boot 自动配置的 bean
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值