spring1-test2-根据bean的类型从IOC容器中获取bean的实例

  • 根据Person这个类型从容器中获取到对象的值,如果该容器中有多个注册对象的话,那么就会出现错误。可以尝试使用第二种方法。
@Test
public void test02(){
    /**
     * 如果IOC容器中的这个类型的bean有多个,那么按照这个类型找会失败。
     */
    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-config.xml");
    Person bean = ioc.getBean(Person.class);
    System.out.println(bean);

    //也可以通过下面这种方式找到唯一对应的Person对象
    Person bean1 = ioc.getBean("person1", Person.class);
    System.out.println(bean1);
}
发布了52 篇原创文章 · 获赞 1 · 访问量 2263

猜你喜欢

转载自blog.csdn.net/Shen_R/article/details/104883396