目錄
- Spring概述
- Spring體系架構
- Core Container(核心容器)
- Data Access/Integration(數據訪問/集成)
- Web
- 其他模塊
- Spring下載
- Spring框架包
- 第三方依賴包
- Spring的入門程序
- 創建Web項目
- 創建包,實現
1. Spring概述
Spring是一個輕量級控制反轉(IoC)和麪向切面(AOP)的容器框架
2. Spring體系架構
2.1. Core Container(核心容器)
- Beans模塊:提供了BeanFactory 。
- Core核心模塊:提供了Spring框架的基本組成部分,包括IoC和DI功能。
- Context上下文模塊:是訪問定義和配置的任何對象的媒介。
- SpEl模塊:是Spring3.0新增模塊,提供Spring Expression Lanuage支持。
2.2. Data Access/Integration(數據訪問/集成)
- JDBC模塊:提供一個JDBC的抽象層。
- ORM模塊:對流行的對象關係映射API提供了集成層支持。
- OXM模塊:提供了一個支持對象/XML映射的抽象層實現。
- JMS模塊:指java消息傳遞服務。
- Transactions模塊:支持對實現特殊接口以及所有POJO類的編程和聲明式的事務管理。
2.3. Web
- WebScoket模塊:提供了WebScoket 和 SockJS的實現。
- Servlet模塊:也稱爲Spring-webmvc模塊。
- Web模塊:提供了基本的Web開發集成特性。
- Portlet模塊:提供了在Portlet環境中使用MVC實現,類似Servlet模塊的功能。
2.4. 其他模塊
- AOP模塊:提供了面向切面編程實現。
- Aspects模塊:提供了與AspectJ的集成功能。
- Instrumentation模塊:提供了類工具的支持和類加載器的實現。
- Messaging模塊:提供對消息傳遞體系結構和協議的支持。
- Test模塊:提供了對單元測試和集成測試的支持。
3.Spring下載
地址:https://repo.spring.io/simple/libs-release-local/org/springframework/spring/
- docs文件夾中包括Spring的API文檔和開發規範。
- libs中包含開發需要的jar包和源碼。
- schema文件夾中包括開發所需要的schema文件,這些文件中定義了spring相關配置文件的約束。
3.1. Spring框架包
libs中有四個基礎包:如下
3.2. 第三方依賴包
使用上圖的基礎包,還需要依賴commons.logging的jar包
地址:http://commons.apache.org/proper/commons-logging/
4.Spring的入門程序
4.1. 在eclipse中一個創建Web項目,將Spring的四個基礎包和commons-logging的jar包複製到Lib目錄下。
4.2. 在src目錄下創建包,面向接口編程
- 在傳統模式下,調用者通常會採用“ new 被調用者 ”的代碼方式來創建對象,這種方式會導致調用者與被調用者之間的耦合性增加,不利於後期項目的升級和維護。
例如:
- 使用Spring框架後,對象的實例不再有調用者來創建,而是由Spring容器來創建,Spring容器負責控制程序之間的關係,而不是由調用者的程序代碼直接控制。這樣,控制權由應用代碼轉移到了Spring容器,控制權發生了反轉,這就是Spring的控制反轉(IoC)。
例如:
- Spring容器負責將被依賴對象賦值個調用者的成員變量,這相當於爲調用者注入了它依賴的實例,這就是Spring的依賴注入(DI)。
實現方式
- 屬性setter方法注入:
例如:
package com.xhh.service.Impl;
import com.xhh.service.HelloWorld;
import com.xhh.service.HelloWorld2;
public class HelloWorldImpl2 implements HelloWorld2 {
// 我需要用到HelloWorld中實現的方法,使用依賴注入實例化
private HelloWorld helloWorld;
// 實現setter注入
public void setHelloWorld(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
@Override
public void say2() {
helloWorld.say();
System.out.println("你好,歡迎來到spring的世界2--依賴注入");
System.out.println("我使用HelloWorld的方法,但我沒有在這個類中實例化它,只是定義出來");
}
}
<?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="helloWorld" class="com.xhh.service.Impl.HelloWorldImpl">
</bean>
<bean id="helloWorld2" class="com.xhh.service.Impl.HelloWorldImpl2">
<!--name值爲HelloWorldImpl2類中未實例的值,ref爲本xml中bean的關於HelloWorld的id -->
<property name="helloWorld" ref="helloWorld"></property>
</bean>
</beans>
package com.xhh.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xhh.service.HelloWorld2;
public class HelloWorldTest2 {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld2 helloWorld2 = (HelloWorld2) applicationContext.getBean("helloWorld2");
helloWorld2.say2();
}
}
- 構造函數注入
例如:
<?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="helloWorld" class="com.xhh.service.Impl.HelloWorldImpl">
</bean>
<bean id="helloWorld2" class="com.xhh.service.Impl.HelloWorldImpl2">
<!--setter注入,name值爲HelloWorldImpl2類中未實例的值,ref爲本xml中bean的關於HelloWorld的id -->
<!-- <property name="helloWorld" ref="helloWorld"></property> -->
<!-- 構造函數注入 -->
<constructor-arg index="0" ref="helloWorld"></constructor-arg>
</bean>
</beans>
package com.xhh.service.Impl;
import com.xhh.service.HelloWorld;
import com.xhh.service.HelloWorld2;
public class HelloWorldImpl2 implements HelloWorld2 {
// 我需要用到HelloWorld中實現的方法,使用依賴注入實例化
private HelloWorld helloWorld;
// 實現setter注入
// public void setHelloWorld(HelloWorld helloWorld) {
// this.helloWorld = helloWorld;
// }
public HelloWorldImpl2(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
@Override
public void say2() {
helloWorld.say();
System.out.println("你好,歡迎來到spring的世界2--依賴注入");
System.out.println("我使用HelloWorld的方法,但我沒有在這個類中實例化它,只是定義出來");
}
}