码迷,mamicode.com
首页 > 编程语言 > 详细

springframework-7IoCContainer

时间:2019-11-03 01:16:45      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:figure   text   com   技术   class   容器   eve   aop   img   

7.1概况

主要包:org.springframework.beans 和 org.springframework.context
主要类:
BeanFactory:提供基本的类的管理的配置机制
ApplicationContext:在BeanFactory的基础上提供 SpringAOP,消息资源管理,事件发布,以及应用层context例如WebApplicationContext

7.2容器概况

ApplicationContext通过加载xml,JavaAnnotations,JavaCode等形式的元数据来实例化,配置和组装Bean
一些开箱即用的ApplicationContext,在单独的应用中常用的有ClassPathXmlApplicationContext 或者 FileSystemXmlApplicationContext.

技术图片

7.2.1配置元数据

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
        https://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>

7.2.2初始化容器

从CLASSPATH加载配置

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

合并xml

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

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

7.2.3使用容器

从容器中获得Bean

// 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();

更灵活的容器

GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();

springframework-7IoCContainer

标签:figure   text   com   技术   class   容器   eve   aop   img   

原文地址:https://www.cnblogs.com/zhouyu0-0/p/11784903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!