Spring Boot 教程:服务组件

服务组件是包含 @Service 注解的类文件。这些类文件可用在与 @RestController 类文件所不同的层来编写业务逻辑。创建服务组件类文件如下所示:
1 public interface ProductService { 2 }
以 @Service 注解实现接口的类如下所示:
1 @Service 2 public class ProductServiceImpl implements ProductService { 3 }
回顾以前的教程中,我们使用产品服务 API(s) 来存储、获取、更新和删除产品。我们在 @RestController 类文件自身中编写业务逻辑。现在我们要把业务逻辑代码从控制器移到服务组件。
你可以创建一个接口,包含增加、编辑、获取和删除方法,示例代码如下:
1 package com.tutorialspoint.demo.service; 2 3 import java.util.Collection; 4 import com.tutorialspoint.demo.model.Product; 5 6 public interface ProductService { 7 public abstract void createProduct(Product product); 8 public abstract void updateProduct(String id, Product product); 9 public abstract void deleteProduct(String id); 10 public abstract Collection<Product> getProducts(); 11 }
以下代码将让你创建一个以 @Service 注解来实现 ProductService 接口的类,用以编写存储、获取、删除和更新产品。
1 package com.tutorialspoint.demo.service; 2 3 import java.util.Collection; 4 import java.util.HashMap; 5 import java.util.Map; 6 import org.springframework.stereotype.Service; 7 import com.tutorialspoint.demo.model.Product; 8 9 @Service 10 public class ProductServiceImpl implements ProductService { 11 private static Map<String, Product> productRepo = new HashMap<>(); 12 static { 13 Product honey = new Product(); 14 honey.setId("1"); 15 honey.setName("Honey"); 16 productRepo.put(honey.getId(), honey); 17 18 Product almond = new Product(); 19 almond.setId("2"); 20 almond.setName("Almond"); 21 productRepo.put(almond.getId(), almond); 22 } 23 @Override 24 public void createProduct(Product product) { 25 productRepo.put(product.getId(), product); 26 } 27 @Override 28 public void updateProduct(String id, Product product) { 29 productRepo.remove(id); 30 product.setId(id); 31 productRepo.put(id, product); 32 } 33 @Override 34 public void deleteProduct(String id) { 35 productRepo.remove(id); 36 37 } 38 @Override 39 public Collection<Product> getProducts() { 40 return productRepo.values(); 41 } 42 }
以里的代码展示了 Rest Controller 类文件,我们用 @Autowired 来自动注入 ProductService 接口并调用其方法。
1 package com.tutorialspoint.demo.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.http.HttpStatus; 5 import org.springframework.http.ResponseEntity; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestBody; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod; 10 import org.springframework.web.bind.annotation.RestController; 11 12 import com.tutorialspoint.demo.model.Product; 13 import com.tutorialspoint.demo.service.ProductService; 14 15 @RestController 16 public class ProductServiceController { 17 @Autowired 18 ProductService productService; 19 20 @RequestMapping(value = "/products") 21 public ResponseEntity<Object> getProduct() { 22 return new ResponseEntity<>(productService.getProducts(), HttpStatus.OK); 23 } 24 @RequestMapping(value = "/products/{id}", method = RequestMethod.PUT) 25 public ResponseEntity<Object> 26 updateProduct(@PathVariable("id") String id, @RequestBody Product product) { 27 28 productService.updateProduct(id, product); 29 return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK); 30 } 31 @RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE) 32 public ResponseEntity<Object> delete(@PathVariable("id") String id) { 33 productService.deleteProduct(id); 34 return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK); 35 } 36 @RequestMapping(value = "/products", method = RequestMethod.POST) 37 public ResponseEntity<Object> createProduct(@RequestBody Product product) { 38 productService.createProduct(product); 39 return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED); 40 } 41 }
POJO class – Product.java 代码在此:
1 package com.tutorialspoint.demo.model; 2 3 public class Product { 4 private String id; 5 private String name; 6 7 public String getId() { 8 return id; 9 } 10 public void setId(String id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 }
主 Spring Boot 应用如下:
1 package com.tutorialspoint.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DemoApplication { 8 public static void main(String[] args) { 9 SpringApplication.run(DemoApplication.class, args); 10 } 11 }
Maven build – pom.xml 代码如下所示:
1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns = "http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 <groupId>com.tutorialspoint</groupId> 9 <artifactId>demo</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <packaging>jar</packaging> 12 <name>demo</name> 13 <description>Demo project for Spring Boot</description> 14 15 <parent> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-parent</artifactId> 18 <version>1.5.8.RELEASE</version> 19 <relativePath/> 20 </parent> 21 22 <properties> 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 25 <java.version>1.8</java.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <build> 42 <plugins> 43 <plugin> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-maven-plugin</artifactId> 46 </plugin> 47 </plugins> 48 </build> 49 50 </project>
Gradle Build – build.gradle 代码如下:
buildscript { ext { springBootVersion = '1.5.8.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.tutorialspoint' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') }
现在你可以使用 Maven 或 Gradle 命令创建可执行 executable JAR 文件并运行 Spring Boot 应用了:
Maven 命令如下:
mvn clean install
在 “BUILD SUCCESS” 之后,你可以在 target 目录下找到 JAR 文件。
Gradle 可以使用以下命令:
gradle clean build
在 “BUILD SUCCESSFUL” 之后,你可以在 build/libs 目录下找到 JAR 文件。
现在,使用以下命令运行 JAR 文件:
java –jar <JARFILE>
应用将在 Tomcat 8080 端口启动,如下图所示:

现在在 POSTMAN 应用中输入以下 URL,可以看到下图所示的输出:
GET API URL 为: http://localhost:8080/products

POST API URL 为: http://localhost:8080/products

PUT API URL 为: http://localhost:8080/products/3

DELETE API URL 为: http://localhost:8080/products/3


浙公网安备 33010602011771号