5.2.3.使用容器

ApplicationContext中调用方法T getBean(String name, Class<T> requiredType)你就可以获得你定义Bean的实例化对象。如下例所示:

  1. // create and configure beans
  2. ApplicationContext context =
  3. new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
  4. // retrieve configured instance
  5. PetStoreService service = context.getBean("petStore", PetStoreService.class);
  6. // use configured instance
  7. List<String> userList = service.getUsernameList();