Spring Boot 2.0快速構(gòu)建服務(wù)組件全步驟
前言
所謂的服務(wù)組件(Service Component)— 就是用于處理系統(tǒng)業(yè)務(wù)邏輯的類,如果按照系統(tǒng)分層設(shè)計理論來劃分,服務(wù)組件是位于業(yè)務(wù)層當(dāng)中的類。在Spring Boot中,服務(wù)組件是一個被**@Service**注解進(jìn)行注釋的類,這些類用于編寫系統(tǒng)的業(yè)務(wù)代碼。在本章節(jié)中,將講解如何創(chuàng)建并使用服務(wù)組件。
在開始正文之前,先來看兩段示例代碼。使用服務(wù)組件之前,我們需要定義服務(wù)組件接口類,用于索引服務(wù)組件提供的服務(wù),代碼如下所示:
public interface UserService{
// TODO ...
}
然后,需要使用**@Service**注解對服務(wù)組件接口實現(xiàn)類進(jìn)行注釋,演示代碼如下:
@Service(value="userService")
public class UserServiceImpl implements UserService{
//TODO ...
}
最后,使用**@Autowired**注解來自動引用服務(wù)組件,代碼如下:
@Controller
public class DemoController{
@Autowired
UserService userService;
//TODO ...
}
在本次講解中,我們依然以對用戶的增、刪、改、查為案例,將控制器中的業(yè)務(wù)方法遷移到服務(wù)組件中。
1. 創(chuàng)建服務(wù)接口
創(chuàng)建一個包含添加用戶、更新用戶、刪除用戶和查詢用戶的服務(wù)接口類 — 用戶服務(wù)組件接口類。詳細(xì)代碼如下:
package com.ramostear.application.service;
import com.ramostear.application.model.User;
import java.util.Collection;
/**
* Created by ramostear on 2019/3/11 0011.
*/
public interface UserService {
/**
* create user
* @param user
*/
void create(User user);
/**
* update user info by ID
* @param id
* @param user
*/
void update(long id,User user);
/**
* delete user by ID
* @param id
*/
void delete(long id);
/**
* query all user
* @return
*/
Collection<User> findAll();
}
2. 實現(xiàn)服務(wù)接口
創(chuàng)建一個接口實現(xiàn)類,用于實現(xiàn)其中的增、刪、改、查四個業(yè)務(wù)方法,并用**@Service**注解進(jìn)行標(biāo)注,具體代碼如下:
package com.ramostear.application.service.impl;
import com.ramostear.application.model.User;
import com.ramostear.application.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @author ramostear
* @create-time 2019/3/11 0011-4:29
* @modify by :
* @since:
*/
@Service(value="userService")
public class UserServiceImpl implements UserService {
private static Map<Long,User> userRepo = new HashMap<>();
@PostConstruct
public void initUserRepo(){
User admin = new User();
admin.setId(1).setName("admin");
userRepo.put(admin.getId(),admin);
User editor = new User();
editor.setId(2).setName("editor");
userRepo.put(editor.getId(),editor);
}
@Override
public void create(User user) {
userRepo.put(user.getId(),user);
}
@Override
public void update(long id, User user) {
userRepo.remove(id);
user.setId(id);
userRepo.put(id,user);
}
@Override
public void delete(long id) {
userRepo.remove(id);
}
@Override
public Collection<User> findAll() {
return userRepo.values();
}
}
3. 使用服務(wù)組件
接下來,定義一個用戶控制器,使用**@Autowired**注解來應(yīng)用用戶服務(wù)組件,實現(xiàn)對用戶的增、刪、改、查功能:
package com.ramostear.application.controller;
import com.ramostear.application.model.User;
import com.ramostear.application.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @author ramostear
* @create-time 2019/3/11 0011-4:42
* @modify by :
* @since:
*/
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/users")
public ResponseEntity<Object> users(){
return new ResponseEntity<>(userService.findAll(), HttpStatus.OK);
}
@PostMapping("/users")
public ResponseEntity<Object> create(@RequestBody User user){
userService.create(user);
return new ResponseEntity<>("User is created successfully.",HttpStatus.CREATED);
}
@PutMapping("/users/{id}")
public ResponseEntity<Object> update(@PathVariable(name="id") long id,@RequestBody User user){
userService.update(id,user);
return new ResponseEntity<>("User is updated successfully.",HttpStatus.OK);
}
@DeleteMapping("/users/{id}")
public ResponseEntity<Object> delete(@PathVariable(name = "id")long id){
userService.delete(id);
return new ResponseEntity<>("User is deleted successfully.",HttpStatus.OK);
}
}
4. 數(shù)據(jù)模型
用戶對象的代碼沿用以往章節(jié)的User.java代碼:
package com.ramostear.application.model;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* @author ramostear
* @create-time 2019/3/6 0006-3:12
* @modify by :
* @since:
*/
@Getter
@Setter
@NoArgsConstructor
public class User {
private long id;
private String name;
public User setId(long id){
this.id = id;
return this;
}
public User setName(String name){
this.name = name;
return this;
}
}
注:應(yīng)用程序主類和Maven build文件與之前章節(jié)的代碼形同,不再列舉。
5. 運行測試
啟動Spring Boot應(yīng)用程序,然后打開Postman測試應(yīng)用程序,分別進(jìn)行如下的測試。
GET 請求:獲取所有的用戶信息。
URL地址:http://localhost:8080/users

獲取用戶信息
POST 請求:新增一位用戶信息
URL地址:http://localhost:8080/users
請求參數(shù):{“id”:3,"name":"reader"}

新增用戶
PUT請求:修改用戶信息
URL地址:http://localhost:8080/users/3
請求參數(shù):{“id”:3,"name":"ramostear"}

修改用戶
DELETE請求:刪除用戶信息
URL地址:http://localhost:8080/users/3
刪除用戶
6. 附件
本章節(jié)用于演示的項目源碼已經(jīng)上傳到Github代碼倉庫,你可以通過下面的地址鏈接免費獲取本章節(jié)的全部源碼信息:
github.com/ramostear/S …(本地下載)
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串示例
這篇文章主要介紹了Java基于動態(tài)規(guī)劃法實現(xiàn)求最長公共子序列及最長公共子字符串,簡單描述了動態(tài)規(guī)劃法的概念、原理,并結(jié)合實例形式分析了Java使用動態(tài)規(guī)劃法求最長公共子序列以及最長公共子字符串相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-08-08
JAVA 多態(tài)操作----父類與子類轉(zhuǎn)換問題實例分析
這篇文章主要介紹了JAVA 多態(tài)操作----父類與子類轉(zhuǎn)換問題,結(jié)合實例形式分析了JAVA 多態(tài)操作中父類與子類轉(zhuǎn)換問題相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下2020-05-05
Spring IOC推導(dǎo)與DI構(gòu)造器注入超詳細(xì)講解
這篇文章主要介紹了Spring IOC推導(dǎo)與DI構(gòu)造器注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
SpringBoot整合MinIO實現(xiàn)文件存儲系統(tǒng)的代碼示例
在現(xiàn)代的應(yīng)用程序中,文件存儲和管理是一個常見的需求,MinIO是一個開源的對象存儲系統(tǒng),與Spring?Boot框架結(jié)合使用,可以快速構(gòu)建高性能的文件存儲系統(tǒng),本文將介紹如何使用Spring?Boot和MinIO來實現(xiàn)文件存儲系統(tǒng)2023-06-06
springmvc不進(jìn)入Controller導(dǎo)致404的問題
這篇文章主要介紹了springmvc不進(jìn)入Controller導(dǎo)致404的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程
Swagger2是一個開源項目,用于為RESTful Web服務(wù)生成REST API文檔,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項目中使用Swagger2及注解解釋的詳細(xì)教程,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Mybatis resultType返回結(jié)果為null的問題排查方式
這篇文章主要介紹了Mybatis resultType返回結(jié)果為null的問題排查方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java?8函數(shù)式接口之Consumer用法示例詳解
這篇文章主要為大家介紹了Java?8函數(shù)式接口之Consumer用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

