Jelajahi Sumber

添加lombok注释替代autowired的示例

tianyunperfect 4 tahun lalu
induk
melakukan
4675bd1a23

+ 5 - 0
springboot-main/src/main/java/com/alvin/controller/AppController.java

@@ -4,7 +4,10 @@ import com.alvin.annotation.ResponseResult;
 import com.alvin.common.entity.PageResult;
 import com.alvin.common.entity.Result;
 import com.alvin.entity.User;
+import com.alvin.service.AppService;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -15,7 +18,9 @@ import java.util.Collections;
 @RestController
 @RequestMapping("/user")
 @ResponseResult
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
 public class AppController {
+    private final AppService appService;
 
     /**
      * 找到一个

+ 0 - 6
springboot-main/src/main/java/com/alvin/controller/BaseController.java

@@ -3,7 +3,6 @@ package com.alvin.controller;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.ModelAttribute;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -15,9 +14,4 @@ public class BaseController {
     protected HttpServletRequest request;
     @Autowired
     protected HttpServletResponse response;
-
-    @ModelAttribute
-    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
-
-    }
 }

+ 4 - 0
springboot-main/src/main/java/com/alvin/service/AppService.java

@@ -0,0 +1,4 @@
+package com.alvin.service;
+
+public interface AppService {
+}

+ 8 - 0
springboot-main/src/main/java/com/alvin/service/impl/AppServiceImpl.java

@@ -0,0 +1,8 @@
+package com.alvin.service.impl;
+
+import com.alvin.service.AppService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class AppServiceImpl implements AppService {
+}