Spring3.0官网文档学习笔记(四)--3.1~3.2.3
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的更多相关文章
- Spring3.0官网文档学习笔记(一)
Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...
- Spring3.0官网文档学习笔记(七)--3.4.2
3.4.2 依赖与配置的细节 3.4.2.1 Straight values (primitives, Strings, and so on) JavaBeans PropertyE ...
- Spring3.0官网文档学习笔记(二)
1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包) spring-*.jar *号代表 ...
- Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6
3.4.3 使用depends-on 使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化. 多个bean之间用","." ...
- android官网文档学习笔记
1.android的四大组件的了大概功能 activity:负责显示界面,和用户交互. service:运行在后台. content provider:为程序app之间的数据访问提供接口. broad ...
- Spring Security 官网文档学习
文章目录 通过`maven`向普通的`WEB`项目中引入`spring security` 配置 `spring security` `configure(HttpSecurity)` 方法 自定义U ...
- mongodb官网文档阅读笔记:与写性能相关的几个因素
Indexes 和全部db一样,索引肯定都会引起写性能的下降,mongodb也没啥特别的,相对索引对读性能的提示,这些消耗通常是能够接受的,所以该加入的索引还是要加入.当然须要慎重一些.扯点远的,以前 ...
- (五)Spring Boot官网文档学习
文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...
- (二)Spring Boot 官网文档学习之入门
文章目录 Spring Boot 是什么 系统要求 Servlet 容器 Maven方式安装Spring Boot 编写第一个 Spring Boot 项目 原文:https://docs.sprin ...
随机推荐
- 【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)
1528: [POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到 ...
- xtuoj 1233 coins(dp)
Coins Accepted : 120 Submit : 305 Time Limit : 1000 MS Memory Limit : 65536 KB Coins Problem Des ...
- 【高斯消元】CDOJ1783 曜酱的线性代数课堂(一)
高斯消元求逆矩阵板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...
- 实验四 Android程序设计-5
点击button按钮,进入下一活动,"the third activity"
- Java学习笔记(9)
final关键字; (修饰符) final关键字的用法: final关键字修饰一个基本类型的变量时,该变量不能重新赋值,第一次的值为最终的. final关键字修饰一个引用类型变量时,该变量不能重新指 ...
- 原生js实现图片轮播思路分析
一.复习原生js实现图片轮播 1.要点 自动轮播 点击小圆圈按钮,显示相应图片 点击左右箭头,实现向前向后轮播图片 2.实现思路 <div id="container"> ...
- Educational Codeforces Round 9 F. Magic Matrix 最小生成树
F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...
- XCode6 ,iOS之PCH文件配置
1: 创建PCH文件 NewFile-->Other中的PCH File-->Next-->Create 2:配置PCH文件 项目中的TARGETS-->Build Setti ...
- JNI小试牛刀
JNI: JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++).从Java1.1开始,JNI标准成为java平台的一 ...
- hdu2263Heavy Cargo
#include <iostream> #include <cstdio> #include <algorithm> #include <queue>/ ...