Spring詳細基本開發流程


LOGO
LOGO

文章已托管到GitHub,大家可以去GitHub查看閱讀,歡迎老板們前來Star! 搜索關注微信公眾號 碼出Offer 領取各種學習資料!

一、Spring概述

1.1 Web開發中的一些問題

  • 傳統Web開發存在硬編碼所造成的過度程序耦合(例如:Service中作為屬性Dao對象)
  • 部分Java EE API較為復雜,使用效率低(例如:JDBC開發步驟)
  • 侵入性強,移植性差(例如:DAO實現的更換,從Connection到SqlSession)

1.2 什么是Spring?

  • Spring 是一個開源的設計層面框架,它解決的是業務邏輯層和其他各層的松耦合問題,因此它將面向接口的編程思想貫穿整個系統應用
  • Spring是眾多優秀設計模式的組合(工廠、單例、代理、適配器、包裝器、觀察者、模板、策略)
  • Spring並未替代現有框架產品,而是將眾多框架進行有機整合,簡化企業級開發,俗稱"膠水框架"

1.3 官網

官方網站:https://spring.io/

GitHub:https://github.com/spring-projects

下載地址:http://repo.spring.io/release/org/springframework/spring/

1.4 Spring架構組成

Spring架構由諸多模塊組成,可分類為

  • 核心技術:依賴注入 ,事件,資源,i18n,驗證,數據綁定,類型轉換,SpEL,AOP
  • 測試:模擬對象,TestContext框架,Spring MVC測試,WebTestClient。
  • 數據訪問:事務 ,DAO支持,JDBC,ORM,封裝XML。
  • Spring MVC和 Spring WebFlux Web框架。
  • 集成:遠程處理,JMS,JCA,JMX,電子郵件,任務,調度,緩存。
  • 語言:Kotlin,Groovy,動態語言。
Spring特點
Spring特點
Spring特點
Spring架構
001
001

1.5 Spring依賴

GroupId ArtifactId 說明
org.springframework spring-beans Beans 支持,包含 Groovy
org.springframework spring-aop 基於代理的AOP支持
org.springframework spring-aspects 基於AspectJ 的切面
org.springframework spring-context 應用上下文運行時,包括調度和遠程抽象
org.springframework spring-context-support 支持將常見的第三方類庫集成到 Spring 應用上下文
org.springframework spring-core 其他模塊所依賴的核心模塊
org.springframework spring-expression Spring 表達式語言,SpEL
org.springframework spring-instrument JVM 引導的儀表(監測器)代理
org.springframework spring-instrument-tomcat Tomcat 的儀表(監測器)代理
org.springframework spring-jdbc 支持包括數據源設置和 JDBC 訪問支持
org.springframework spring-jms 支持包括發送/接收JMS消息的助手類
org.springframework spring-messaging 對消息架構和協議的支持
org.springframework spring-orm 對象/關系映射,包括對 JPA 和 Hibernate 的支持
org.springframework spring-oxm 對象/XML 映射(Object/XML Mapping,OXM)
org.springframework spring-test 單元測試和集成測試支持組件
org.springframework spring-tx 事務基礎組件,包括對 DAO 的支持及 JCA 的集成
org.springframework spring-web web支持包,包括客戶端及web遠程調用
org.springframework spring-webmvc REST web 服務及 web 應用的 MVC 實現
org.springframework spring-webmvc-portlet 用於 Portlet 環境的MVC實現
org.springframework spring-websocket WebSocket 和 SockJS 實現,包括對 STOMP 的支持
org.springframework spring-jcl Jakarta Commons Logging 日志系統

二、自定義工廠

編寫一個讀取Spring配置文件並創建對象的工廠,了解Spring工廠創建對象的流程,以便使用自定義工廠來測試Spring基本開發流程

package com.mylifes1110.factory;

import java.io.IOException;
import java.util.Properties;

/**
 * @ClassName MyFactory
 * @Description 自定義工廠(創建對象)
 * @Author Ziph
 * @Date 2020/7/12
 * @Since 1.8
 * @Version 1.0
 */


public class MyFactory {
    private Properties properties = new Properties();

    public MyFactory() {
    }

    public MyFactory(String config) throws IOException {
        // 加載配置文件
        properties.load(MyFactory.class.getResourceAsStream(config));
    }

    // 獲取對象
    public Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        String classPath = properties.getProperty(beanName);
        if (classPath != null) {
            Class clazz = null;
            clazz = Class.forName(classPath);
            return clazz.newInstance();
        }
        return null;
    }
}

三、Spring基本開發步驟

3.1 創建Maven項目

如果對Maven不了解的小伙伴可以參考Maven教程

File -> NewProject
image-20200712164104354
image-20200712164104354
創建Maven項目
image-20200712164221187
image-20200712164221187

3.2 引入依賴

在pom.xml文件中引入Spring常用依賴

<dependencies>
    <!-- Spring常用依賴 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
</dependencies>

3.3 創建Spring空配置文件

在resources文件夾中創建一個名為spring-context.xml,命名並無規定,還有其他的常用命名,比如:applicationContext.xmlbeans.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">

</beans>

3.4 定義需要生產的Bean對象

定義一個Bean對象、生產該對象並測試該對象內的方法(UserDaoImpl)

package com.mylifes1110.dao.impl;

import com.mylifes1110.bean.User;
import com.mylifes1110.dao.UserDao;

public class UserDaoImpl implements UserDao {

    @Override
    public int insertUser(User user) {
        System.out.println("------insertUser and UserDao------");
        return 0;
    }
}

3.5 基本依賴注入

依賴注入在這里可以把它理解為將要生產的對象注入到Spring容器中,也就是在spring-context.xml文件利用標簽注入,這樣就可以讓Spring知道你要生產的對象是誰

標簽: <bean id="唯一標簽" class="需要被創建的目標對象全限定名"/>

<?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="UserDao" class="com.mylifes1110.dao.impl.UserDaoImpl"/>

</beans>

3.6 調用Spring工廠創建對象

調用Spring工廠API接口ApplicationContext讀取配置Spring核心配置文件並創建工廠對象

package com.mylifes1110.dao;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserDaoImplTest {
    /**
     * @MethodName insertUser
     * @Param []
     * @Description 測試使用Spring工廠獲取對象
     * @Author Ziph
     * @Date 2020/7/12
     */

    @Test
    public void insertUser() {
        // 讀取配置文件所需創建對象中所需創建的bean對象並獲取spring工廠對象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        // 通過注入時的唯一標識(id)獲取bean對象
        UserDao userDao = (UserDao) context.getBean("UserDao");
        // 使用對象
        userDao.insertUser(null);    // 輸出結果為:------insertUser and UserDao------
    }
}

四、依賴與配置文件詳解

4.1 Spring的依賴關系

Spring框架包含多個模塊,每個模塊各司其職,可結合需求引入相關依賴Jar包實現功能。

注意: Jar包彼此存在依賴,只需引入最外層Jar即可由Maven自動將相關依賴Jar引入到項目中。

Spring常用功能的Jar包依賴關系
image-20191230164517693
image-20191230164517693

4.2 schema

配置文件中的頂級標簽中包含了語義化標簽的相關信息(spring-context.xml頭文件即是schema)

注意: Spring需要導入的schema標簽是很有規律的,因為Spring是特別規范的。例如:如下context別名的schema標簽是我自己復制修改的,它也是我們所用到Spring的schema,你可以復制beans的schema來替換所有beans的關鍵字即可,但是記得復制全(注意查看三個有context標志標簽語句,另外“ : ”后面的是別名哦)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       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

       http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xsd"
>

</beans>
schema名稱 描述
xmlns 語義化標簽所在的命名空間
xmlns:xsi XMLSchema-instance 標簽遵循Schema標簽標准
xsi:schemaLocation xsd文件位置,用以描述標簽語義、屬性、取值范圍等


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM