3.1 Spring IoC容器与Beans简单介绍

    BeanFactory接口提供对随意对象的配置;

    ApplicationContext是BeanFactory的子接口。整合了Spring Aop功能,消息资源控制。事件公布,应用层特殊的上下文(在web应用中)

    由IoC容器实例化、组装、管理的对象都是Bean

3.2 容器概述

    org.springframework.context.ApplicationContext代表Spring IoC容器,而且负责通过读取配置元数据来实例化、配置、组装Bean。配置元数据能够通过三种形式表示:xml,Java凝视,Java code(这个不懂)

    在独立的环境中。实例化ClassPathXmlApplicationContext或FileSystemXmlApplicationContext对象。

3.2.1 配置元数据

    除了配置在xml文件里,还有两种方法:

    Annotation-based configuration: ?

    Java-based configuration: ?

你能够使用Spring's integration with AspectJ配置不在IoC容器控制下的对象?

基于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-3.0.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>

3.2.2 实例化一个容器

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

Note:第4章有更方便的机制读取XML文件?

3.2.2.1 基于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>

services.xml必须位于包括该文件的xml文件同个文件夹或者是在classPath文件夹下。

能够通过相对路径“../path”这样的方式来配置文件路径,可是这方法并不推荐(用这样的方式创建出来的是依赖文件是在当前应用的外部?)。尤其不推荐用于"classpath:"这样的URL中(执行时解决程序会选择“近期的”classpth,并查看它的父文件夹,导致找不到相应的xml文件)。

能够使用:"file:C:/config/services.xml"或"classpath:/config/services.xml"。可是要记住假设使用绝对路径的话,就存在耦合问题了。

3.2.3 使用容器

// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}); // retrieve configured instance
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class); // use configured instance
List userList service.getUsernameList();

Spring3.0官网文档学习笔记(四)--3.1~3.2.3的更多相关文章

  1. Spring3.0官网文档学习笔记(一)

    Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...

  2. Spring3.0官网文档学习笔记(七)--3.4.2

    3.4.2 依赖与配置的细节     3.4.2.1  Straight values (primitives, Strings, and so on)     JavaBeans PropertyE ...

  3. Spring3.0官网文档学习笔记(二)

    1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包)     spring-*.jar *号代表 ...

  4. Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6

    3.4.3 使用depends-on     使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化. 多个bean之间用","." ...

  5. android官网文档学习笔记

    1.android的四大组件的了大概功能 activity:负责显示界面,和用户交互. service:运行在后台. content provider:为程序app之间的数据访问提供接口. broad ...

  6. Spring Security 官网文档学习

    文章目录 通过`maven`向普通的`WEB`项目中引入`spring security` 配置 `spring security` `configure(HttpSecurity)` 方法 自定义U ...

  7. mongodb官网文档阅读笔记:与写性能相关的几个因素

    Indexes 和全部db一样,索引肯定都会引起写性能的下降,mongodb也没啥特别的,相对索引对读性能的提示,这些消耗通常是能够接受的,所以该加入的索引还是要加入.当然须要慎重一些.扯点远的,以前 ...

  8. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

  9. (二)Spring Boot 官网文档学习之入门

    文章目录 Spring Boot 是什么 系统要求 Servlet 容器 Maven方式安装Spring Boot 编写第一个 Spring Boot 项目 原文:https://docs.sprin ...

随机推荐

  1. [BZOJ3224]普通平衡树(旋转treap,STL-vector)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 20328  Solved: 8979[Submit][St ...

  2. bzoj 2137: submultiple

                                                     Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 23 ...

  3. 【状压dp】送餐员

    [odevs2800]送餐员 题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份东西,分别送达n个不同的客户的手上.n个不同的客户分别在1~n个编号的城市中.送外卖的从0号 ...

  4. Java程序运行时内存划分

    1.Java程序跨平台运行的原因 主要原因是:各种平台的JVM和字节码文件 Java源程序--具体平台的机器代码文件---被编译器翻译成平台无关的Class文件,又用特定JVM运行字节码文件,JVM在 ...

  5. PHP数组和数据结构(上)

    1.数组的声明 两种方法: 直接给数组元素赋值 使用array()函数声明 说明: (1)索引数组的下标可以是非连续的值,只要在初始化时指定非连续的下标值即可 如果指定的下标值已经声明过,则属于对变量 ...

  6. Linux限制某些用户或IP登录SSH、允许特定IP登录SSH

    说明:一般要实现这种功能时,先安装VPN,然后客户端登录VPN,然后通过内网IP登录SSH. 搭建OpenVPN: 参考:http://www.cnblogs.com/EasonJim/p/83338 ...

  7. .net 中两个日期算经过的月份数

    DateTime startDate = DateTime.Parse("2014-11-1"); DateTime endDate = DateTime.Parse(" ...

  8. andriod inputType

    <EditText Android:layout_width="fill_parent" android:layout_height="wrap_content&q ...

  9. iOS:进度条控件的详细使用

    进度条控件:UIProcessView:UIView   功能:顾名思义,用来显示下载进度或者传输数据进度.   属性: @property(nonatomic) UIProgressViewStyl ...

  10. 如何查看Oracle日志

    Oracle日志查看 一.Oracle日志的路径: 登录:sqlplus "/as sysdba" 查看路径:SQL> select * from v$logfile; SQ ...