1. 程式人生 > 實用技巧 >Spring 框架(持續完善中)

Spring 框架(持續完善中)


目錄標題


一、Spring 框架

什麼是框架[framework]?

在某個問題域中的一整套解決方案,在javaee體系下,不同的問題域[場景]下面有不同的框架,比如:在持久層中有 mybatis, Hibernate, Jdo等框架,還有MVC的框架,如:struts2, spring mvc、JSF、等。


Spring 是什麼?

直譯過來就是“春天”,它是由作者Rod Johnson 為改變早期企業級專案開發過程中使用EJB所遇到的問題而開發的一套輕量級的企業應用開發解決方案,取名 Spring, 【意味著程式設計師的“春天”】

現在, Spring Framework 是整個JAVAEE開發生態中必不可少的一個環節,大量的JAVAEE專案都是基於Spring框架來展開的,Spring Framework也構建了一個很好的技術生態,基本上可以提供給程式設計師一整套的技術鏈來完成企業級專案的開發。

Spring Framework 目前最新版本是 5.x, 它的技術生態所含蓋的技術和框架也很多,主要有:

  1. Spring MVC 框架
  2. Spring Boot
  3. Spring Cloud
  4. Spring Security
  5. Spring cache


Spring Framework 核心概念

  1. IOC 容器, 也叫 Inverse of Control, 控制反轉, 換成另外一個詞更好理解,叫 依賴注入 Dependency Injection, 簡稱 DI。
  2. AOP, 面向切面程式設計
  3. 資料訪問支援層,包含 JDBC的技術, mybatis的支援,hibernate的支援, JPA 的支援.
  4. 事務的支援
  5. 測試的支援


瞭解Spring 框架的架構圖


從上圖可以看出,Spring core Container 是支撐所有上面技術元件的基礎,其中beans是負責建立由spring管理的各種物件,context是容器應用上下文, SPEL是表示式語言,core 就是框架的核心程式碼碼,一般無需程式設計師參與。

test 是測試元件,也是所有上面技術的元件所共用的。

左上角是資料持久層的支援,包含 JDBC、ORM[hibernate框架和mybatis框架]、OXM[java object和xml之間的轉換]、以及JMS的支援、當然還有事務的支援。

右上角是 針對MVC的實現,由 spring mvc負責。




二、Spring Framework 之 IOC

開發的步驟流程

  1. 準備一個maven專案
  2. 匯入相關的依賴 [spring-context]和[spring-test]
  3. 編寫相關的程式碼
  4. 測試


Spring IOC的特性

  1. 採用單例模型,也就是說在IOC容器中,預設只建立配置Bean的唯一例項
  • 通過 scope 屬性來控制容器是否採用單例,預設是 單例 [singleton]
  • 設定 scope=“prototype” ,表示IOC容器採用 原型模式,也就是不是單例。
  1. 採用“懶載入“方式來建立目標Bean物件
  2. 同一個介面型別,有多個實現的情況下,就會產生“二義性”。怎麼解決?
  • 再加一個 名字 進行限定。

所以,預設情況下, Spring IOC中,Bean的搜尋是根據 型別 來搜尋的[by Type], 如果型別存在“二義性”時,則需要進一步通過bean的名稱[id] 來搜尋,這個叫 by Name。


IOC容器核心API

  1. ApplicationContext 介面,這就是IOC的核心容器介面,通過它可以獲取被IOC容器管理的Bean
  1. 支援兩種不同的配置:

  • XML 配置,傳統的配置,一直很強大,很好用,如下: [ 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 定義我們的Bean, 在此定義的Bean,就會由spring容器負責建立和管理 -->
        <bean id="studentService" scope="singleton" init-method="xxx"
              class="com.hc.service.impl.StudentServiceImpl"/>
    </beans>
    
  • 註解的配置,JDK5.0之後才支援的,現在很流行。 如下: [ AppConfig.java ]

    @Configuration   //表示這是一個spring 註解配置類
    @ComponentScan(value = {"com.hc.service.impl"})
    public class AppConfig {
    
        //nothing!
    
    }
    
  • ApplicationContext 的實現類

 ​	\\- ClassPathXmlApplicationContext    如果採用xml配置,使用這個類

 ​	\\- AnnotationConfigApplicationContext  [簡稱 ACAC], 如果使用註解配置,使用這個類


三、Spring-test 環境

  1. 如果你想手動編寫程式碼來獲取Spring IOC 的上下文,這種情況下,是無需Spring-test 元件的,如下:

    
    /******
     * 測試[非 Spring-test 環境]
     */
    public class StudentServiceTest {
        
        @Test
        public void testAdd_use_spring_annotation() {
            //手工寫程式碼得到 ac [也就是 spring  ioc 上下文]
            ApplicationContext ac =
                    new AnnotationConfigApplicationContext(AppConfig.class);
            //通過 ac 來獲取被IOC容器管理的bean
            IStudentService iss = ac.getBean(IStudentService.class);
            //
            iss.add(null);
        }
    }
    

  1. 當然,建議是使用 spring-test 元件來做單元測試,它需要使用如下註解
  • @ContextConfiguration(classes=xxxx.class)
  • @RunWith(SpringJUnit4ClassRunner.class)
 ```java
 @ContextConfiguration(classes = AppConfig.class)  //讀取spring的註解配置類
 @RunWith(SpringJUnit4ClassRunner.class)   //把上面讀取到的IOC上下文由測試環境來使用
 public class StudentServiceTestWithSpring {
     @Autowired
     private IStudentService studentService;
     @Test
     public void testAdd_use_spring_annotation_test() {
         System.out.println(studentService);
         studentService.add(null);
     }
 }
 ```

  • 這種使用 spring-test 元件進行測試的方式,是官方推薦的。它還可以這麼寫:

    @ContextConfiguration(classes = AppConfig.class)  //讀取spring的註解配置類
    public class StudentServiceTestWithSpring extends AbstractJUnit4SpringContextTests {
        @Autowired
        private IStudentService studentService;
        @Test
        public void testAdd_use_spring_annotation_test() {
            System.out.println(studentService);
            studentService.add(null);
        }
    }
    

IOC相關的註解

  1. @Component 元件,修飾類的,需要被容器管理的bean型別,現在使用很少,因為有下面三個來代替它:
    * @Service 從語義上擴充套件了@Component, 代表 服務層的bean
    * @Repository 從語義上擴充套件了@Component, 代表 DAO層的bean
    * @Controller 從語義上擴充套件了@Component, 代表 控制層的bean
  1. @Autowired 自動注入,它是 byType 進行搜尋的。
  1. @Qualifier 進一步限定搜尋的Bean,按姓名搜尋,也就是 byName
  1. @Resource 它是JAVAEE規範中的註解,spring IOC 實現了這個註解,它即支援byType,也支援byName
  1. @Configuration 表示此類是一個Spring的註解配置類
  1. @ComponentScan 用來指定要掃描的包,可以指定多個
  1. @ContextConfiguration 它是spring-test的註解,用在測試類上面,來讀取目標配置類
  1. @RunWith 也是spring-test的註解,用在測試類上面,讓測試類擁有spring 框架的上下文環境。


有關資料來源配置

資料來源是用來配置資料庫的連線的,它是自動參與到連線池管理之中,一般都無需我們去實現資料來源,都是由第三方的元件提供的,目前最主流的資料來源技術元件【連線池實現】有:

  • commons-dbcp 元件, 由 apache 提供的
  • druid 元件,由 alibaba 提供
  • hikari 元件,一家日本公司

我們選用 druid 資料來源,所以,在pom.xml中要加入 druid的依賴.

配置資料來源相關的屬性

  • url
  • driver
  • username
  • password


Note:
歡迎點贊,留言,轉載請在文章頁面明顯位置給出原文連結
知者,感謝您在茫茫人海中閱讀了我的文章
沒有個性 哪來的簽名!
詳情請關注點我
持續更新中

© 2020 09 - Guyu.com | 【版權所有 侵權必究】