zoukankan      html  css  js  c++  java
  • Spring Boot

    前言

    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
    ...
    ...
    ...
    
  • 相关阅读:
    sosreport -a --report
    笔记本用HDMI转VGA 使用双屏办公 听语音
    1080p就是1920*1080,2k就是2560*1440,4k就是3840*2160
    linux命令截取文件最后n行(所有命令)
    http://www.loongnix.org/index.php/Lbrowser
    tar解压某个目录 tar解压某个指定的文件或者文件夹
    解析CentOS 8上的Xrdp服务器安装
    CentOS、RHEL、Asianux、Neokylin、湖南麒麟、BC Linux、普华、EulerOS请参考“1.1 CentOS本地源配置”;
    Ubuntu18.04制作本地源
    js获取页面高度
  • 原文地址:https://www.cnblogs.com/chenpi/p/9785938.html
Copyright © 2011-2022 走看看