The ApplicationContext
is the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. Using the method T getBean(String name, Class requiredType)
you can retrieve instances of your beans.
The ApplicationContext
enables you to read bean definitions and access them as follows:
ApplicationContext
是一个高级工厂的接口,能够维护不同bean及其依赖关系的注册表。 使用方法T getBean(String name,Class requiredType)
你可以检索bean的实例。
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();
You use getBean()
to retrieve instances of your beans. The ApplicationContext
interface has a few other methods for retrieving beans, but ideally your application code should never use them. Indeed, your application code should have no calls to the getBean()
method at all, and thus no dependency on Spring APIs at all. For example, Spring’s integration with web frameworks provides for dependency injection for various web framework classes such as controllers and JSF-managed beans.
你使用getBean()
来检索bean的实例。 ApplicationContext
接口有一些其他方法来检索bean,但理想情况下你的应用程序代码不应该使用它们。 事实上,你的应用程序代码应该根本没有调用getBean()
方法,因此根本没有依赖于Spring API。 例如,Spring与Web框架的集成并为各种Web框架类(如控制器和JSF管理的bean)提供了依赖注入。