springbootが起動したときにどのbeanを注入したかを確認します

1585 ワード

プログラムエントリに参加するには、次の手順に従います.
@SpringBootApplication
public class SpringbootFirstApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootFirstApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

  • プログラム出力:
    Let’s inspect the beans provided by Spring Boot:  basicErrorController  beanNameHandlerMapping  beanNameViewResolver  characterEncodingFilter  commandLineRunner  conventionErrorViewResolver  defaultServletHandlerMapping  defaultViewResolver  dispatcherServlet  dispatcherServletRegistration  duplicateServerPropertiesDetector  embeddedServletContainerCustomizerBeanPostProcessor  error  errorAttributes  errorPageCustomizer  errorPageRegistrarBeanPostProcessor
    ….  ….
    プログラムが起動するとspringbootは40-50個のbeanを自動的に注入する.