import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); String[] beanNames = context.getBeanDefinitionNames(); for (String beanName : beanNames) { System.out.println(beanName); } } }
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); String[] beanNames = context.getBeanDefinitionNames(); for (String beanName : beanNames) { System.out.println(beanName); } } }In this example, we define an ApplicationContext using the AnnotationConfigApplicationContext class and a Java configuration class called "AppConfig". Then, we use the getBeanDefinitionNames() method to get a list of all bean names and we print them out. Both examples use the Spring Framework package library, specifically the org.springframework.context package.