码迷,mamicode.com
首页 > 编程语言 > 详细

Spring项目获取注入bean集合

时间:2017-08-15 23:00:44      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:java   spring   

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");*/

}

}


}


本文出自 “姚小二” 博客,请务必保留此出处http://yaogz.blog.51cto.com/10000833/1956535

Spring项目获取注入bean集合

标签:java   spring   

原文地址:http://yaogz.blog.51cto.com/10000833/1956535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!