Spring之Chaper 4, The IoC container-4.2 Container view(容器总述)
来源:互联网 发布:windows选择哪个 编辑:程序博客网 时间:2024/04/28 19:30
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="itemDao" ref="itemDao"/> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for services go here --></beans>
4.2 Container View(容器概述)
org.springframework.context.ApplicationContext接口代表Spring的IoC容器并且负责上面提及beans的实例化,配置和装配。容器通过读取配置元数据获取要实例化,配置和装配的对象的指令。可以通过XML,注解或者Java代码表示配置元数据。其允许你表达这样的对象来构成应用程序和这样对象之间的相互依赖。
ApplicationContext接口的几个实现由Spring提供开箱即用。在独立的应用程序中,通常是创建一个ClassPathXmlApplicationContext
或者ClassPathXmlApplicationContext
的实例。其中XML是定义配置元数据的传统格式,你可以命令容器使用Java注解或者代码作为元数据格式,通过提供少量的XML配置来申明支持额外的元数据格式。
在大多数应用程序场景中,显示的用户代码不要求实例化一个或者多个Spring IoC容器。例如,在一个web应用程序场景中,应用程序的web.xml文件中仅仅8行样板化的web描述符就能满足要求。如果使用SpringSource Tool Suite工具,容器生成样板化的配置。
下面的图表是Spring如何工作的高级视图。你的应用程序类通过配置元数据联合,这样在创建和初始化ApplicationContext之后,你有一个全配置和可执行的系统或者应用程序。
图 4-1 The Spring IoC Container
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> <bean/> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --></beans>
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="itemDao" ref="itemDao"/> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for services go here --></beans>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao"> <!-- additional collaborators and configuration for this bean go here --> </bean> <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao"> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for data access objects go here --></beans>
Composing XML-based configuration metadata(基于XML配置的元数据)
定义跨越多个xml文件是很用的。一般每个独立的xml配置文件在你的结构中表示一个逻辑层或者模块。
你可以使用application Context构造器从xml片段中加载定义的beans。这个构造器需要多个Resource位置,如同前面显示的那样。作为可选地,使用一个或者多个<import/>元素从其他文件中加载定义的beans。例如:
<beans> <import resource="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/></beans>
messageSource.xml
, 和themeSource.xml。所有的位置路径与导入的定义文件是相对的,这样service.xml必须与importing的文件在同一个目录或者类位置下,messageSource.xml
, 和themeSource.xml必须位于resources所在的目录下。如你所见,忽略了斜线,所给的路径都是相对的,不用斜线是一中较好的形式。文件内容就被导入了,包括顶层元素<beans/>必须通过Spring语法校验。
注意:
4.2.3 Using the container(使用容器)
ApplicationContext是一个高级工厂的接口,能够维护注册不同beans和其依赖。使用方法T getBean(String name, Class<T> requiredType) 你可以重新获得beans的实例。
ApplicationContext能使你读取bean和访问它们,如下所示:
// create and configure beansApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});// retrieve configured instancePetStoreService service = context.getBean("petStore", PetStoreService.class);// use configured instanceList<String> userList = service.getUsernameList();
使用getBean()重新获得beans的实例。ApplicationContext接口还有其他的方法重新获取这些beans,但是理想情况下,你的应用程序代码应该从不会使用它们。
确实,你的应用程序代码应该从不调用getBean()方法,这样就与Spring的API没有依赖。例如,Spring与web框架的集成,对不同的web框架类提供了依赖注入,比如控制器和JSF-管理的bean。
- Spring之Chaper 4, The IoC container-4.2 Container view(容器总述)
- 【Java.Spring.Core】【IoC】IoC容器 - Container
- The IoC Container 控制反转容器
- Chaper 11. Associate Container
- Configuring Beans in the Spring IoC Container
- Spring的容器(container)
- 初识Spring IoC container
- laravel的 容器container(ioc)
- Laravel 4 IoC Container
- Laravel 4 IOC container
- 4、Container Extension Points(spring容器的扩展)
- Introduction to the Spring IoC container and beans
- Container View(sb-容器视图)
- spring-reference-The IoC container的翻译笔记(自己用)
- STL组件(components)之容器(container)
- Rhyme/Spring 1.2.3. Using the container 容器的使用
- Custom Container View Controller(自定义容器视图控制器)
- Part III. Core Technologies--Chapter 4, The IoC container(4.1~4.4)
- (9)javaEE---equest与response
- servlet工作原理分析
- 自己设计并使用断言
- GIT基本概念和用法总结
- POJ 2112Optimal Milking 最大流
- Spring之Chaper 4, The IoC container-4.2 Container view(容器总述)
- javaweb 验证码制作
- Bash字符串处理(获取文件名和后缀名)
- Android四大组件之一----Broadcast学习心得
- ETL调度开发(5)——连接数据库执行数据库命令子程序
- 加值电话吸金 声讯电话商机
- 响一声电话吸费 手机回拨吸费 无需经验
- hdu 4946
- 什么加值电话 声讯电话商机 月收入两万