Spring boot application load beans as per dependencies add in pom.xml file and classes uses in application. if you are new then you should read this post first how to create spring boot application.
Get all beans in spring boot application :
You just need to implement CommandLineRunner and get ApplicatationContext object using @Autowire annotation. now get all beans from application context using getBeanDefinitionNames() method.
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.context.ApplicationContext;
@SpringBootApplication
public class HelloWorldApplication implements CommandLineRunner{
@Autowired
private ApplicationContext appContext;
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
public void run(String... arg0) throws Exception {
String[] beans = appContext.getBeanDefinitionNames();
Arrays.sort(beans);
for (String bean : beans) {
System.out.println(bean);
}
}
}
Get specific bean in spring boot application :
If you want to get specific bean of spring boot application then call getbean(Classname.class) of application context.
package com.jp.helloWorld;
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.context.ApplicationContext;
import com.jp.helloWorld.controller.HelloController;
@SpringBootApplication
public class HelloWorldApplication implements CommandLineRunner{
@Autowired
private ApplicationContext appContext;
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
public void run(String... arg0) throws Exception {
HelloController controller = appContext.getBean(HelloController.class);
}
}