Spring 2.0启动快速构建服务组件全步骤

  

  

所谓的服务组件(服务组件)——就是用于处理系统业务逻辑的类,如果按照系统分层设计理论来划分,服务组件是位于业务层当中的类,在春天引导中,服务组件是一个被* * @ Service * *注解进行注释的类,这些类用于编写系统的业务代码。在本章节中,将讲解如何创建并使用服务组件。
  

  

在开始正文之前,先来看两段示例代码。使用服务组件之前,我们需要定义服务组件接口类,用于索引服务组件提供的服务,代码如下所示:
  

        公共接口UserService {//TODO……   }      

然后,需要使用* * @ service * *注解对服务组件接口实现类进行注释,演示代码如下:
  

        @ service (value=" https://www.yisu.com/zixun/userService ")   公共类UserServiceImpl实现UserService {//TODO……   }      

最后,使用* * @ autowired * *注解来自动引用服务组件,代码如下:
  

        @ controller   公开课DemoController {   @ autowired   UserService UserService;//TODO……   }      

在本次讲解中,我们依然以对用户的增,删,改,查为案例,将控制器中的业务方法迁移到服务组件中。

  


  

  

创建一个包含添加用户,更新用户,删除用户和查询用户的服务接口类——用户服务组件接口类。详细代码如下:
  

        包com.ramostear.application.service;      进口com.ramostear.application.model.User;      进口java.util.Collection;/* *   *由ramostear>   包com.ramostear.application.service.impl;      进口com.ramostear.application.model.User;   进口com.ramostear.application.service.UserService;   进口org.springframework.stereotype.Service;      进口javax.annotation.PostConstruct;   进口java.util.Collection;   进口java.util.HashMap;   进口java.util.Map;/* *   * @author ramostear   * @create-time 2019/3/11 0011 - 29   * @modify:   * @since:   */@ service (value=" https://www.yisu.com/zixun/userService ")   公共类UserServiceImpl实现UserService {      私有静态Map<长,User>,userRepo=new HashMap<的在();      @PostConstruct   公共空间initUserRepo () {   用户管理=新用户();   admin.setId (1) .setName (“admin”);   userRepo.put (admin.getId()、管理);      用户编辑=新用户();   editor.setId (2) .setName(“编辑器”);   编辑userRepo.put (editor.getId ());   }   @Override   公共空间创建用户(用户){   userRepo.put (user.getId()、用户);   }      @Override   公共空间更新(长id、用户用户){   userRepo.remove (id);   user.setId (id);   userRepo.put (id、用户);   }      @Override   公共空间删除(id) {   userRepo.remove (id);   }      @Override   公共CollectionfindAll () {   返回userRepo.values ();   }   }      


  

  

接下来,定义一个用户控制器,使用* * @ autowired * *注解来应用用户服务组件,实现对用户的增,删,改,查功能:
  

        包com.ramostear.application.controller;      进口com.ramostear.application.model.User;   进口com.ramostear.application.service.UserService;   进口org.springframework.beans.factory.annotation.Autowired;   进口org.springframework.http.HttpStatus;   进口org.springframework.http.ResponseEntity;   进口org.springframework.web.bind.annotation。*;/* *   * @author ramostear   * @create-time 2019/3/11 0011 -各自   * @modify:   * @since:   */@RestController   公开课用户控件{      @ autowired   UserService UserService;         @GetMapping("/用户”)   公共ResponseEntity用户(){   返回新ResponseEntity<祝辞(userService.findAll (), HttpStatus.OK);   }      @PostMapping("/用户”)   公共ResponseEntity创建(@RequestBody用户用户){   userService.create(用户);   返回新ResponseEntity<祝辞(“用户创建成功。”HttpStatus.CREATED);   }      @PutMapping("/用户/{id} ")   公共ResponseEntity更新(@PathVariable (name=" id ") id, @RequestBody用户用户){   userService.update (id、用户);   返回新ResponseEntity<祝辞(“用户更新成功。”HttpStatus.OK);   }      @DeleteMapping("/用户/{id} ")   公共ResponseEntity

Spring 2.0启动快速构建服务组件全步骤

Copyright © 2020-2023 feiqueyun.cn. All Rights Reserved. 肥雀云_南京肥雀信息技术有限公司版权所有 南京肥雀信息技术有限公司 苏ICP备16063723号-5