import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {


private static ApplicationContext ctx;


public static void main(String[] args) {

try {

ctx = new ClassPathXmlApplicationContext(

new String[] { "classpath:spring/spring-context.xml", "classpath:spring/spring-mvc.xml" });

listAllSpringBeans();

} catch (Exception e) {

e.printStackTrace();


}

}


private static void listAllSpringBeans() {

String[] beanNames = ctx.getBeanDefinitionNames();

int allBeansCount = ctx.getBeanDefinitionCount();


System.out.println("所有beans的数量是:" + allBeansCount);

for (String beanName : beanNames) {

Class<?> beanType = ctx.getType(beanName);

Package beanPackage = beanType.getPackage();


Object bean = ctx.getBean(beanName);


System.out.println("BeanName:" + beanName);

/*System.out.println("Bean的类型:" + beanType);

System.out.println("Bean所在的包:" + beanPackage);


System.out.println("\r\n");*/

}

}


}