使用容器

ApplicationContext 是一個維護不同 bean 和依賴性的註冊的先進的工廠接口。使用方法:T getBean(String name, Class<T> requiredType) ,你可以從 beans 裏面獲取實例。

你可以通過ApplicationContext讀取和使用 bean ,就想下面的例子:
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml""daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();


你可以通過getBean()方法從bean裏面獲取一個實例化對象。ApplicationContext還有幾個其他的方法可以獲取實例化對象, 但是在應用中不要去使用這些方法。真的,你的應用代碼不應該調用getBean()方法,因此,在Spring的API裏面毫無依賴性。比如,Spring 整合的Web框架爲各式各樣的Web框架提供依賴注入,例如控制層和 JSF-managed beans。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章