Print all the Spring beans that are loaded

前端 未结 8 1751
说谎
说谎 2020-11-28 03:05

Is there a way to print all the spring beans that are loaded on startup?I am using Spring 2.0.

8条回答
  •  被撕碎了的回忆
    2020-11-28 03:50

    Print all bean names and its classes:

    package com.javahash.spring.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class HelloWorldController {
    
        @Autowired
        private ApplicationContext applicationContext;
    
        @RequestMapping("/hello")
        public String hello(@RequestParam(value="key", required=false, defaultValue="World") String name, Model model) {
    
            String[] beanNames = applicationContext.getBeanDefinitionNames();
    
            for (String beanName : beanNames) {
    
                System.out.println(beanName + " : " + applicationContext.getBean(beanName).getClass().toString());
            }
    
            model.addAttribute("name", name);
    
            return "helloworld";
        }
    }
    

提交回复
热议问题