获取bean的姓名
获取bean的名称
<bean id="personService" class="com.zhen.service.impl.PersonServiceBean" />
获取bean的名称:
1.PersonService ps = (PersonService)ctx.getBean("personService");
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
2.
BeanFactory ctx = new ClassPathXmlApplicationContext("bean.xml"); PersonService ps = (PersonService)ctx.getBean("personService"); BeanFactory 和ApplicationContext都可以获取到bean的名称: ApplicationContext和BeanFacotry相比,提供了更多的扩展功能,但其主要区别在于后者是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用调用getBean方法才会抛出异常;而ApplicationContext则在初始化自身是检验,这样有利于检查所依赖属性是否注入;所以通常情况下我们选择使用ApplicationContext.