I am trying to list all the managed Components in a Spring Boot application using a ListableBeanFactory interface as illustrated in How to Get All Spring-Managed Beans?
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
displayAllBeans();
}
public static void displayAllBeans() {
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
for(String beanName : allBeanNames) {
System.out.println(beanName);
}
}
}
When running the application I get the following Exception indicating that the applicationContext
object is null
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
at mygroup.myapp.displayAllBeans(myapp.java:18)
at mygroup.myapp.main(myapp.java:14)
... 5 more
What am I missing? Thanks in advance
UPDATE:
The code from the example works as expected. There was a mistake in my code that prevented the applicationContext
variable to be set up correctly.