一、IoC 概述
IoC(控制反轉:Inverse of Control)是Spring 容器的核心,與DI(依賴注入:Dependency Injection)是同一個意思。
從注入方法上看,主要可以劃分爲三種類型:構造函數注入、屬性注入和接口注入。Spring 支持構造函數注入和屬性注入。
構造函數注入
在構造函數注入中,我們通過調用類的構造函數,將接口實現類通過構造函數變量傳入。由於是構造函數,就必須注入,沒有選擇性。
屬性注入
屬性注入可以有選擇地通過Setter 方法完成調用類所需依賴的注入,更加靈活方便。
接口注入
將調用類所有依賴注入的方法抽取到一個接口中,調用類通過實現該接口提供相應的
注入方法。爲了採取接口注入的方式,必須先聲明一個接口,由於通過接口注入需要額外聲明一個接口,增加了類的數目,而且它的效果和屬性注
入並無本質區別。
二、Spring 3的命名空間
可以訪問:http://www.springframework.org/schema/
查看所有的schema文件,和對應的命名空間。
常用的命名空間如下:
spring-beans-3.0.xsd 最主要的命名空間,也是默認命名空間,用於配置Bean。
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
簡化屬性配置
p命名空間,設置屬性值的格式爲 p:屬性名="值"
http://www.springframework.org/schema/p
(由於屬性名是可變的,所以沒有對應的Schema文件)
spring-tx-3.1.xsd 聲明式事務配置
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
spring-context-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
spring-mvc-3.0.xsd Spring MVC的配置
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
spring-aop-3.0.xsd AOP的配置
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
spring-util-3.1.xsd 簡化某些複雜的標準配置
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
spring-jdbc-3.1.xsd Spring內嵌數據庫的配置
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
spring-jee-3.1.xsd 爲了簡化EJB,JDNI提供的
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
spring-jpa.xsd jpa配置
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
spring-jms-3.0.xsd JMS配置
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
spring-lang-3.0.xsd JRuby,Groovy動態語言配置
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
spring-oxm-3.0.xsd 配置對象XML映射
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
spring-task-3.0.xsd 任務調度
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
spring-tool-3.0.xsd 可集成Spring一些有用的工具
http://www.springframework.org/schema/tool
http://www.springframework.org/schema/tool/spring-tool-3.0.xsd