Spring入門介紹 Spring入門介紹

摘自:https://www.cnblogs.com/ljw1994/p/11820321.html

Spring入門介紹

 

概述

  • 下載地址:https://repo.spring.io/release/org/springframework/spring/
  • spring是開源的輕量級框架
  • spring核心的主要兩部分
    • AOP :面向切面編程,擴展功能而不修改源代碼
    • IOC :控制反轉,對象的創建通過spring配置來實現
  • spring是一站式框架
    • web :springMVC
    • service :IOC
    • dao :JDBC

IOC

IOC的底層原理

  • 創建xml配置文件,配置要創建對象的類
    <bean id="userSevice" class="com.ljw.service.UserService">
  • bean標籤的屬性
    • id:值不能有特殊字符
    • class:要配置的類的路徑
    • scope:創建對象的方式
      • singleton:默認值,單例
      • prototype:多例
    • name:跟id類似,但可以有特殊字符
  • 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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
</beans>
  • 創建工廠類,使用dom4j解析xml配置文件+反射,得到該類的實例
    IOC原理

屬性注入(DI)

  • DI:依賴注入,向類裏面的屬性設置值
  • set方法
    1. 在類中定義該屬性的set方法
    2. spring_context.xml 配置文件中配置相關的設置
  • 有參構造
    1. 定義有參構造方法
    2. spring_context.xml 配置文件中配置相關的設置
  • 對象屬性注入
    1. 將要注入對象設置爲類的屬性
    2. spring_context.xml 配置文件中配置相關的設置
      spring_context.xml中的配置:
      spring_context.xml

測試:
test

  • 複雜類型的注入
    1. Array
    2. List
    3. Map
    4. properties
    1572178214073_26.png

Spring 的 bean 管理(註解)

  • 註解詳情請查看文章:註解 @Annotation

    一、導入 jar 包

    複雜數據類型注入

二、創建 Spring 配置文件,導入約束

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
</beans>

三、再 Spring 中開啓註解掃描

開啓註解掃描

  • 如果有多個包,就填寫共有的包名

四、註解創建對象

1572178213540_18.png

4.1 創建對象有四個註解

  • @Component
  • @Controller
  • @Service
  • @Repository
  • 目前這四個註解的功能是一致的,都是創建對象,只是爲了讓標註類本身的用途清晰

4.2 創建對象的方式

  • @Scope(value="singleton") 單實例
  • @Scope(value="prototype") 多實例
    創建對象

五、註解注入屬性

  1. 創建service類,創建dao類,再service中得到dao對象
    • 用註解創建 dao 對象和 service 對象
      dao對象
      service對象

    • 再service類中創建dao類型的屬性,並用註解注入對象,有兩種方式
      第一種:@Autowired
      @Autowired

    第二種:@Resource
    @Resource

    • 測試
      • image.png

AOP

一、AOP概念

  • AOP:面向切面(方面)編程
  • AOP採取橫向抽取機制,取代了傳統縱向繼承體系重複性代碼

二、AOP原理

AOP原理

三、AOP操作術語

  • Joinpoint(連接點): 類裏面可以被增強的方法,這些方法稱爲連接點(可以被增強的方法)
  • Pointcut(切入點):所謂切入點是指我們要對哪些Joinpoint進行攔截的定義.
  • Advice(通知/增強):所謂通知是指攔截到Joinpoint之後所要做的事情就是通知.通知分爲前置通知,後置通知,異常通知,最終通知,環繞通知(切面要完成的功能,在方法之前和之後執行(比如:計算程序運行的時間))
  • Aspect(切面): 是切入點和通知(引介)的結合
  • Introduction(引介):引介是一種特殊的通知在不修改類代碼的前提下, - Introduction可以在運行期爲類動態地添加一些方法或Field.
  • Target(目標對象):代理的目標對象(要增強的類)
  • Weaving(織入):是把增強應用到目標的過程.
    • 把advice 應用到 target的過程
  • Proxy(代理):一個類被AOP織入增強後,就產生一個結果代理類

四、使用AspectJ實現AOP

1. 介紹

  • AspectJ不是Spring的一部分,是和Spring一起使用進行AOP操作

2. 實現AOP的兩種方式

  • 基於AspectJ的XML配置
  • 基於AspectJ的註解方式

3. 實現步驟

  • AOP操作準備
    • 導入基本jar包和AOP相關的jar包
      jar包

    • 導入AOP約束
    <?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
    </beans>
  • 使用表達式配置切入點
    • 切入點:實際增強的方法
    • 常用的表達式:execution 表達式
  • 增強類
package com.ljw.spring.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component(value="userAdvice")
public class UserAdvice {
    
    /**
     * @description 前置通知
     */
    public void userBefore() {
        System.out.println("前置通知........");
    }
    
    /**
     * @description 後置通知
     */
    public void userAfter() {
        System.out.println("後置通知........");
    }
    
    /**
     * @description 環繞通知
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    public void userAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 方法之前
        System.out.println("方法之前..........");
        
        // 執行增強的方法
        proceedingJoinPoint.proceed();
        
        // 方法之後
        System.out.println("方法之後..........");
    }
}
  • 配置Spring配置文件
    • 導入約束
      • image.png
    • 配置AOP操作
      • image.png

Spring整合web項目

一、實現原理

  1. 通過 new 對象,功能可以實現,但效率很低
    • image.png
  1. 實現思想:把加載配置文件和創建對象過程,在服務器啓動的時候完成

  2. 具體實現原理 (1) ServletContext對象 (2) 監聽器 (3) 具體步驟
    • 在服務器啓動的時候,爲每個項目創建一個ServletContext對象
    • 在 ServletContext 對象創建的時候,使用監聽器可以監聽 ServletContext對象什麼時候創建
    • 當監聽器監聽到 ServletContext 對象創建的時候,加載 spring 配置文件,創建配置文件對象
    • 把創建出來的對象放到 ServletContext 域對象裏面(setAttribute 方法)
    • 獲取對象(getAttribute 方法)

知識點

ServletContext 對象

一、介紹

  • ServletContext官方叫servlet上下文。服務器會爲每一個工程創建一個對象,這個對象就是ServletContext對象
  • 這個對象全局唯一,而且工程內部的所有servlet都共享這個對象。所以叫全局應用程序共享對象。

二、作用

  • 是一個域對象
  • 可以讀取全局配置參數
  • 可以搜索當前工程目錄下面的資源文件
  • 可以獲取當前工程名字(瞭解)

三、域對象

  1. 域對象介紹
  • 域對象是服務器在內存上創建的存儲空間,用於在不同動態資源(servlet)之間傳遞與共享數據。
  1. 怎樣得到 ServletContext 對象
  • ServletContext servletContext = config.getServletContext();
  • ServletContext servletContext = this.getServletContext();
  1. 域對象方法
  • 凡是域對象都有如下三個方法
    • image.png

監聽器 Listener

一、監聽器模型

  • 事件:用戶對組件的一個操作,或者說程序執行某個方法,稱之爲一個事件,如機器人程序執行工作。
  • 事件源:發生事件的組件就是事件源,也就是被監聽的對象,如機器人可以工作,可以跳舞,那麼就可以把機器人看做是一個事件源。
  • 事件監聽器(處理器):監聽並負責處理事件的方法,如監聽機器人工作情況,在機器人工作前後做出相應的動作,或者獲取機器人的狀態信息。

二、執行步驟

  1. 給事件源註冊監聽器。
  2. 組件接受外部作用,也就是事件被觸發。
  3. 組件產生一個相應的事件對象,並把此對象傳遞給與之關聯的事件處理器。
  4. 事件處理器啓動,並執行相關的代碼來處理該事件。

execution表達式

一、語法

  • execution(<訪問修飾符>?<返回類型><方法全名>(<參數>)<異常>)

二、常用的寫法

  • execution(* com.ljw.spring.aop.User.add(..)) 對User類的add方法增強
  • execution(* com.ljw.spring.aop.User.*(..)) 對User類的所有方法增強
  • execution(* *.*(..)) 對所有方法增強
  • execution(* save*(..)) 對所有save開頭的方法增強
 
 
標籤: spring, java
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章