1. 程式人生 > >企業分布式微服務雲SpringCloud SpringBoot mybatis (十八)springboot在啟動時註入了哪些bean

企業分布式微服務雲SpringCloud SpringBoot mybatis (十八)springboot在啟動時註入了哪些bean

contex 測試 gree names system com clas temp ice

在程序入口加入:

@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);
            }

        };
    }

}

  

程序輸出:

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.

通過@RunWith() @SpringBootTest開啟註解:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {

    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    }

    @Test
    public void getHello() throws Exception {
        ResponseEntity<String> response = template.getForEntity(base.toString(),
                String.class);
        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
    }
}

  

運行它會先開啟sprigboot工程,然後再測試,測試通過

技術分享圖片源碼來源

企業分布式微服務雲SpringCloud SpringBoot mybatis (十八)springboot在啟動時註入了哪些bean