Spring架構---Spring入門,spring架構---spring
Spring入門
為了能更好的理解先講一些有的沒的的東西:
什麼是Spring
Spring是分層的JavaSE/EE full-stack(一站式) 輕量級開源架構
分層
SUN提供的EE的三層結構:web層、業務層、資料訪問層(持久層/整合層)
Struts2是web層基於MVC設計模式架構
Hibernate是持久層的一個ORM的架構
一站式
Spring架構有對三層的每層解決方案
web層:Spring MVC
持久層:JDBC Template
業務層:Spring的Bean管理
Spring的核心:
控制反轉意思就是說,當我們調用一個方法或者類時,不再有我們主動去建立這個類的對象,控制權交給別人(spring)。
依賴注入意思就是說,spring主動建立被調用類的對象,然後把這個對象注入到我們自己的類中,使得我們可以使用它。
Spring優點
方便解耦,簡化開發
Spring就是一個大工廠,可以將所有對象建立和依賴關係維護,交給Spring管理
AOP編程的支援
Spring提供面向切面編程,可以方便的實現對程式進行許可權攔截、運行監控等功能
聲明式事務的支援
只需要通過配置就可以完成對事務的管理,而無需手動編程
方便程式的測試
Spring對Junit4支援,可以通過註解方便的測試Spring程式
方便整合各種優秀架構
Spring不排斥各種優秀的開源架構,其內部提供了對各種優秀架構(如:Struts、Hibernate、MyBatis、Quartz等)的直接支援
降低JavaEE API的使用難度
Spring 對JavaEE開發中非常難用的一些API(JDBC、JavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大降低
前面講的都是廢話,下面開始進入最基礎的:
Spring的入門的程式
(1)導包
下載Spring的開發包
下載最新的jar包地址:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/
spring-framework-4.3.7.RELEASE-dist.zip ---Spring開發包
注意:
spring-beans-4.3.7.RELEASE.jar //這四個在spring開發包中檔案搜尋就可以找到
spring-context-4.3.7.RELEASE.jar
spring-core-4.3.7.RELEASE.jar
spring-expression-4.3.7.RELEASE.jar
開發的日誌記錄的包: //在apache下下載
com.springsource.org.apache.commons.logging-1.1.1.jar --- 用於整合其他的日誌的包(類似Hibernate中slf4j)
com.springsource.org.apache.log4j-1.2.15.jar
(2)建立Spring的設定檔
在src下建立一個applicationContext.xml
引入XML的約束:在spring開發包中找到xsd-config.html.引入beans約束
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
(3)建立介面:HelloServcie
public interface HelloServcie { public void sayHello();}
(4)建立介面實作類別:
1 public class HelloServiceImpl implements HelloServcie {2 3 public void sayHello() {4 System.out.println("hello spring");5 }6 }
(5)配置applicationContext.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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here --> <!-- 1.快速入門程式============================================================================== --> <!-- 配置使用哪個實作類別 id 標識符 class 需要執行個體化的類路徑 --> <bean id="helloServices" class="com.study.spring.a_quickstart.HelloServiceImpl"> </bean> <!-- end.快速入門程式============================================================================== --> </bean
(6)配置測試類別
public class HelloTest { public static void main(String[] args) { //傳統寫法 緊密耦合 HelloServiceImpl servcie =new HelloServiceImpl(); //手動調用方法 servcie.sayHello(); //工廠+反射+設定檔 ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml"); //通過工廠 和反射一級設定檔中的資訊載入類對象 HelloServcie helloServcie=(HelloServcie) applicationContext.getBean("helloServices"); helloServcie.sayHello(); }}/*後台兩次輸出:hello spring */
這個例子非常簡單,也主要是從這個例子說明spring的第一個優點那就是解耦合,這樣更有利於後期的維護。
這篇文章寫得很入門,也希望大家多多指點,謝謝!