iT邦幫忙

2024 iThome 鐵人賽

DAY 2
0

Spring框架

首先我們先來了解Spring 框架 是什麼
Spring Framework 的概念最初由 Rod Johnson 在他 2002 年出版的書 "Expert One-on-One J2EE Design and Development" 中提出

主要是用來簡化 Java 開發的過程,其中結合了MVC架構、DI、AOP、容器等等的概念

這裡我們針對以下的核心概念進行補充:

  1. DI (依賴注入)
  2. IoC (反轉控制)

  1. DI
    DI 最主要是用來降低程式間的耦合度,他透過導入的方式讓每個類別之間可以被自動注入,而不是手動註冊,這樣的差異使得程式具有更高的活動性以及可維護性。

以下會介紹一個簡單的範例,演示這個 DI 應該要怎麼做使用:

// MessageService 接口  
public interface MessageService {  
    String getMessage();  
}  

// MessageService 的實現  
@Service  
public class EmailService implements MessageService {  
    public String getMessage() {  
        return "Hello World from Email Service!";  
    }  
}  

// 使用 MessageService 的類  
@Component  
public class MessagePrinter {  
    private final MessageService messageService;  

    // 構造函數注入 (DI)  
    @Autowired  
    public MessagePrinter(MessageService messageService) {  
        this.messageService = messageService;  
    }  

    public void printMessage() {  
        System.out.println(messageService.getMessage());  
    }  
}  

這個範例中,可以看到首先會建立一個接口,並在這個接口中撰寫應該要擴寫的功能,接著使用一個類別當作特定功能的去擴寫這個接口的功能,最後在一個主要功能的類別中將這個特定功能的類別使用 @Autowired 自動注入,最後只需要在主要程式碼的部分將已經加進Spring容器的 service呼叫出來,就可以直接使用了

  1. IoC
    IoC是一種設計原則,它將程式中的控制流程轉移到外部框架或容器中。一般需要控制程式的流程和對象的創建,但是在IoC中,這些責任被轉移到了外部實體,上面介紹的 DI 其實就是實現 IoC 的其中一種方法

承接上面的範例,其實上面的範例已經差不多完成IoC了,由於在建立類別時有添加了 @Component ,所以這個類別會被註冊到一個容器內,因此我們要做的就只是將它從這個容器內拿出來做使用

// 定義接口  
public interface MessageService {  
    String getMessage();  
}  

// 實現類,使用@Service註解將其註冊為Spring bean  
@Service  
public class EmailService implements MessageService {  
    public String getMessage() {  
        return "Email message";  
    }  
}  

// 使用者類,使用@Component註解將其註冊為Spring bean  
@Component  
public class MessagePrinter {  
    private final MessageService messageService;  

    // 使用@Autowired進行依賴注入  
    @Autowired  
    public MessagePrinter(MessageService messageService) {  
        this.messageService = messageService;  
    }  

    public void printMessage() {  
        System.out.println(messageService.getMessage());  
    }  
}  

// 配置類  
@Configuration  
@ComponentScan  
public class AppConfig {  
}  

// 主應用類  
public class Application {  
    public static void main(String[] args) {  
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);  
        MessagePrinter printer = context.getBean(MessagePrinter.class);  
        printer.printMessage();  
    }  
}  

可以看到在主應用的部分,我們將這個類別使用getBean的方式取出,接著就可以使用這個類別裡面的功能


總結

其實Spring 框架的概念就是將程式碼做模塊化,最終目的就是為了要降低整體程式碼的耦合度以及維護需要的成本,像這種做法就延伸出了各式各樣的概念,形成了一套生態系統
例如:

  1. Spring Boot: 簡化Spring應用的初始搭建和開發過程。
  2. Spring Data: 簡化資料庫訪問。
  3. Spring Security: 提供安全框架。
  4. Spring Cloud: 提供雲原生應用開發的工具。

在本次的系列教學文章中我們會提到的有「Spring Boot、Spring Data、Spring Security」,我們會利用這些建立一套安全的後端系統,並且讓前端接取相關的API服務


上一篇
[DAY 01] 前言
系列文
智慧語義互動平台:基於Spring和Semantic Kernel的Android應用創新2
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言