【Java.Spring.Core】【IoC】IoC容器 - Container

IoC也称为依赖注入。

org.springframework.beans和org.springframework.context包是Spring IoC容器的基础。BeanFactory接口提供了可以管理任何类型对象的配置机制。AplicationContext是BeanFactory接口的子接口,它增加了AOP特征,message resource handling, event publication,以及特定应用层的context上下文如web应用中的WebApplicationContext。

简而言之,BeanFactory提供了基本的功能,而ApplicationContext则增加了更多的企业应用的功能。

在Spring中,有Spring IoC容器管理的对象称为Beans

容器

接口org.springframework.context.ApplicationContext表示Spring IoC容器,负责实例化,配置,装配前面提到的beans。

容器通过读取配置文件/元数据(metadata)来获取用来实例化对象的规范。配置元数据(metadata)可能是XML文件,Java注解(annotations),甚至是Java Code。


Spring本身提供了一些ApplicationContext接口的实现类;例如在一个stand-alone应用中,通常会创建一个ClassPathXmlApplicationContext或者FileSystemXmlApplicationContext对象。XML是常用的配置元数据形式。

在大部分应用中,通常不需要显式地去实例化一个Spring IoC 容器。例如,在web应用中,可以通过在web.xml通过少量的配置来完成context的创建。



配置元数据(Configuration metadata)

配置元数据用来通知Spring 容器何如去实例化,配置,装配对象。

XML文件是最常见的配置元数据形式


Spring配置元数据中通常包含多个bean的定义。在XML形式的配置元数据中,beans被配置在元素<bean/>中,这些XML元素都位于上层XML元素节点<beans/>中。

这些定义的bean通常为服务层(service layer)对象,如DAOs;表示层对象,如Structs Action实例;基础层对象,如Hibernate SessionFactor, JMS Queue等。通常不在容器中定义具体的业务对象


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">

    <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>


id属性用来表示这个bean;class属性为bean类型的完整类型名称。


bean定义可以位于多个XML文件中,使用<import/>元素来加载其他的配置文件,例如:

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>


这些包含的文件的路径为相对于当前配置文件的相对路径;即services.xml与当前文件位于同一个目录,而messageSource.xml和themeSource.xml文件位于子目录resources中(路径前面的 / 将被忽略)。同时import进来的XML文件同样需要包含上层的<beans>元素,并且遵循同样的配置文件Schema。


实例化容器

实例化容器只需要向ApplicationContext的构造函数中传入配置元数据的资源路径,例如:

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

使用容器

ApplicationContext接口是一个对象厂,负责维护管理benas及其依赖。

使用方法 T getBean(String name, Class<T> requiredType)可以用来获取实例化的benas。

例如:

// 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()方法,具体的注入方法参见后面。









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值