Spring Framework之IoC容器

org.springframework.beansorg.springframework.context包是Spring框架的IoC容器的基础。

BeanFactory 提供了一种高级配置机制,能够管理任何类型的对象。 

ApplicationContext 是的子接口BeanFactory 它增加了:

  • 与Spring的AOP功能轻松集成

  • 消息资源处理(用于国际化)

  • 活动发布

  • 应用层特定的上下文,例如WebApplicationContext 用于Web应用程序中的。

简而言之,BeanFactory提供了配置框架和基本功能,并ApplicationContext增加了更多针对企业的功能。

org.springframework.context.ApplicationContext接口代表Spring IoC容器,并负责实例化,配置和组装Bean。

容器通过读取配置元数据来获取有关要实例化,配置和组装哪些对象的指令。

ApplicationContextSpring提供了接口的几种实现通常创建ClassPathXmlApplicationContext 或的实例 FileSystemXmlApplicationContext

使用容器

// create and configure beans

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance

PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance

List<String> userList = service.getUsernameList();

猜你喜欢

转载自www.cnblogs.com/cngsl/p/11985288.html