Print all loaded Spring beans

Is there a way to print all spring beans that load at startup? I am using spring 2.0.

+48
java spring
Mar 07 2018-12-12T00:
source share
5 answers

Yes, get ApplicationContext and call .getBeanDefinitionNames()

You can get the context:

  • implementation of ApplicationContextAware
  • introducing it @Inject / @Autowired (after 2.5)
  • use WebApplicationContextUtils.getRequiredWebApplicationContext(..)

Related: You can also detect every bean registration by registering a BeanPostprocessor bean. It will be notified for each bean.

+54
Mar 07 '12 at 13:50
source share
 public class PrintBeans { @Autowired ApplicationContext applicationContext; public void printBeans() { System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames())); } } 
+39
Jun 04 '14 at
source share

Print all the <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"; } } 
+13
Mar 07 '15 at 20:28
source share

Using Spring Boot and Drive Starter

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 

you can check the endpoint /beans

+7
Aug 09 '16 at 12:29
source share

You can try calling

 org.springframework.beans.factory.ListableBeanFactory.getBeansOfType(Object.class) 

Or enable debug org.springframework for org.springframework .

+3
Mar 07 2018-12-12T00:
source share



All Articles