【问题标题】:View all Spring Configuration files loaded so far查看目前加载的所有 Spring 配置文件
【发布时间】:2021-02-25 20:53:56
【问题描述】:

我的项目由几个 @Configuration 注释的 bean 配置类组成。

如何打印出所有已加载的配置类?

我已阅读这些问题,但它们似乎不适用于我的用例:

【问题讨论】:

    标签: java spring


    【解决方案1】:

    您可以使用ApplicationContext#getBeansWithAnnotation。它不仅会采用带有@Configuration 注释的类,而且还会采用带有@Configuration 元注释的注释的类,例如@SpringBootApplication

    @Bean
    ApplicationRunner applicationRunner(ApplicationContext applicationContext) {
      return args -> {
        applicationContext.getBeansWithAnnotation(Configuration.class)
          .entrySet()
          .stream()
          .filter(entry -> entry.getValue().getClass().getPackage().getName().startsWith("com.your.package"))
          .forEach(entry -> {
          System.out.println("name: " + entry.getKey() + ", bean: " + entry.getValue());
        });
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2022-11-06
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多