Ver Fonte

@Autowired改构造方法注入

skyline há 2 anos atrás
pai
commit
f5c6ba2745

+ 2 - 3
admin/src/main/java/com/kym/admin/controller/CustomController.java

@@ -8,7 +8,6 @@ import com.kym.service.miniapp.ChargeOrderService;
 import com.kym.service.miniapp.PayLogService;
 import com.kym.service.miniapp.UserService;
 import com.kym.service.miniapp.WalletDetailService;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 /**
@@ -44,8 +43,8 @@ public class CustomController {
     @SysLog("查询用户充电订单列表")
     @GetMapping("/listChargeOrders")
     public R listChargeOrders(@ModelAttribute CustomChargeOrdersQueryParam params,
-                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
-                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
+                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
+                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
         return R.success(customChargeOrders.customChargeOrders(params, pageNum, pageSize));
     }
 

+ 0 - 2
common/src/main/java/com/kym/common/utils/AESUtil.java

@@ -2,9 +2,7 @@ package com.kym.common.utils;
 
 
 import com.kym.common.config.EnPlusConfig;
-import jakarta.annotation.PostConstruct;
 import org.apache.tomcat.util.codec.binary.Base64;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.crypto.Cipher;

+ 7 - 5
miniapp/src/main/java/com/kym/miniapp/controller/AccountController.java

@@ -1,9 +1,7 @@
 package com.kym.miniapp.controller;
 
 import com.kym.common.R;
-import com.kym.common.annotation.ApiLog;
 import com.kym.service.miniapp.WalletDetailService;
-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.RequestParam;
@@ -21,16 +19,20 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/account/")
 public class AccountController {
 
-    @Autowired
-    private WalletDetailService walletDetailService;
+    private final WalletDetailService walletDetailService;
+
+    public AccountController(WalletDetailService walletDetailService) {
+        this.walletDetailService = walletDetailService;
+    }
 
     /**
      * 钱包明细
+     *
      * @param type
      * @return
      */
     @GetMapping("/walletDetail")
-    public R walletDetail(@RequestParam(name = "type", defaultValue = "0",required = false) int type) {
+    public R walletDetail(@RequestParam(name = "type", defaultValue = "0", required = false) int type) {
         return R.success(walletDetailService.listWalletDetail(type));
     }
 

+ 13 - 12
miniapp/src/main/java/com/kym/miniapp/controller/ChargerController.java

@@ -3,7 +3,6 @@ package com.kym.miniapp.controller;
 import com.alibaba.fastjson2.JSONObject;
 import com.kym.common.R;
 import com.kym.common.annotation.ApiLog;
-import com.kym.entity.admin.Station;
 import com.kym.entity.admin.vo.StationVo;
 import com.kym.entity.enplus.response.EnResponse;
 import com.kym.entity.miniapp.ChargeOrder;
@@ -13,7 +12,6 @@ import com.kym.service.enplus.EnPlusService;
 import com.kym.service.miniapp.ChargeOrderService;
 import com.kym.service.miniapp.ChargeService;
 import lombok.SneakyThrows;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -29,20 +27,23 @@ import java.util.List;
 @RequestMapping("/charge")
 public class ChargerController {
 
-    @Autowired
-    private StationService stationService;
+    private final StationService stationService;
 
-    @Autowired
-    private EnPlusService enPlusService;
+    private final EnPlusService enPlusService;
 
-    @Autowired
-    private ChargeService chargeService;
+    private final ChargeService chargeService;
 
-    @Autowired
-    private ChargeOrderService chargeOrderService;
+    private final ChargeOrderService chargeOrderService;
 
-    @Autowired
-    private EnNotifyService enNotifyService;
+    private final EnNotifyService enNotifyService;
+
+    public ChargerController(StationService stationService, EnPlusService enPlusService, ChargeService chargeService, ChargeOrderService chargeOrderService, EnNotifyService enNotifyService) {
+        this.stationService = stationService;
+        this.enPlusService = enPlusService;
+        this.chargeService = chargeService;
+        this.chargeOrderService = chargeOrderService;
+        this.enNotifyService = enNotifyService;
+    }
 
     /**
      * 充电站列表

+ 5 - 4
miniapp/src/main/java/com/kym/miniapp/controller/CommonController.java

@@ -1,9 +1,7 @@
 package com.kym.miniapp.controller;
 
 import com.kym.common.R;
-import com.kym.common.annotation.ApiLog;
 import com.kym.service.admin.ContactService;
-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;
@@ -17,8 +15,11 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/common/")
 public class CommonController {
 
-    @Autowired
-    private ContactService contactService;
+    private final ContactService contactService;
+
+    public CommonController(ContactService contactService) {
+        this.contactService = contactService;
+    }
 
     @GetMapping("/contact")
     R contact() {

+ 5 - 3
miniapp/src/main/java/com/kym/miniapp/controller/PaymentController.java

@@ -6,7 +6,6 @@ import com.kym.common.annotation.ApiLog;
 import com.kym.service.wechat.WxPayService;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.SneakyThrows;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
@@ -19,8 +18,11 @@ import org.springframework.web.bind.annotation.*;
 @RequestMapping("/payment")
 public class PaymentController {
 
-    @Autowired
-    private WxPayService wxPayService;
+    private final WxPayService wxPayService;
+
+    public PaymentController(WxPayService wxPayService) {
+        this.wxPayService = wxPayService;
+    }
 
     @ApiLog("微信支付")
     @PostMapping("/wxPay")

+ 9 - 7
miniapp/src/main/java/com/kym/miniapp/controller/UserController.java

@@ -10,7 +10,6 @@ import com.kym.entity.miniapp.params.WxLoginParams;
 import com.kym.entity.miniapp.vo.UserVo;
 import com.kym.service.miniapp.CollectService;
 import com.kym.service.miniapp.UserService;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
@@ -26,14 +25,17 @@ import org.springframework.web.bind.annotation.*;
 @RequestMapping("/user")
 public class UserController {
 
-    @Autowired
-    UserService userService;
+    private final UserService userService;
 
-    @Autowired
-    private CollectService collectService;
+    private final CollectService collectService;
 
-    @Autowired
-    private IDGenerator idGenerator;
+    private final IDGenerator idGenerator;
+
+    public UserController(UserService userService, CollectService collectService, IDGenerator idGenerator) {
+        this.userService = userService;
+        this.collectService = collectService;
+        this.idGenerator = idGenerator;
+    }
 
     /**
      * 微信登录/注册

+ 3 - 3
service/src/main/java/com/kym/service/enplus/impl/EnPlusServiceHelper.java

@@ -14,7 +14,6 @@ import org.springframework.stereotype.Service;
 @Service
 @CacheConfig
 public class EnPlusServiceHelper {
-    // TODO: 2023-08-02
     /*
      * 原因:
      * Spring 缓存注解是基于Spring AOP切面,必须走代理才能生效,同类调用或者子类调用父类带有缓存注解的方法时属于内部调用,没有走代理,所以注解不生效。
@@ -24,11 +23,12 @@ public class EnPlusServiceHelper {
     private EnPlusService enPlusService;
 
     @Autowired
-    public  void setEnPlusService(EnPlusService enPlusService){
+    public void setEnPlusService(EnPlusService enPlusService) {
         this.enPlusService = enPlusService;
     }
 
-    @Cacheable(cacheNames = "EN_PLUS" ,key = "#root.methodName") //有个坑,@Cacheable 注解在对象内部调用不会生效,参考:https://blog.csdn.net/zh452647457/article/details/86487423
+    @Cacheable(cacheNames = "EN_PLUS", key = "#root.methodName")
+    //有个坑,@Cacheable 注解在对象内部调用不会生效,参考:https://blog.csdn.net/zh452647457/article/details/86487423
     public String queryToken() {
         return enPlusService.queryToken();
     }

+ 7 - 11
service/src/main/java/com/kym/service/enplus/impl/EnPlusServiceImpl.java

@@ -17,18 +17,13 @@ import com.kym.service.enplus.EnPlusService;
 import okhttp3.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
-import org.yaml.snakeyaml.Yaml;
 
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.time.LocalDateTime;
-import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -42,21 +37,22 @@ public class EnPlusServiceImpl implements EnPlusService {
     public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
     private static final Logger LOGGER = LoggerFactory.getLogger(EnPlusServiceImpl.class);
     static OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder().build();
+    private final RedisTemplate<String, String> redisTemplate;
+    @Lazy
+    private final EnPlusServiceHelper enPlusServiceHelper;
     @Value("${en-plus.operatorId}")
     private String OperatorId;
     @Value("${en-plus.operatorSecret}")
     private String OperatorSecret;
     @Value("${en-plus.sigSecret}")
     private String SigSecret;
-    @Autowired
-    private RedisTemplate<String, String> redisTemplate;
 
-    @Autowired
-    @Lazy
-    private EnPlusServiceHelper enPlusServiceHelper;
+    public EnPlusServiceImpl(RedisTemplate<String, String> redisTemplate, EnPlusServiceHelper enPlusServiceHelper) {
+        this.redisTemplate = redisTemplate;
+        this.enPlusServiceHelper = enPlusServiceHelper;
+    }
 
     public static <T> T parse(String json, Class<T> clz) {
-        // TODO: 2023-08-01 json异常处理
         return JSONObject.parseObject(json, clz);
     }
 

+ 0 - 5
service/src/main/java/com/kym/service/miniapp/impl/AccountServiceImpl.java

@@ -5,8 +5,6 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.kym.entity.miniapp.Account;
 import com.kym.mapper.miniapp.AccountMapper;
 import com.kym.service.miniapp.AccountService;
-import com.kym.service.miniapp.WalletDetailService;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 /**
@@ -21,9 +19,6 @@ import org.springframework.stereotype.Service;
 @DS("db-miniapp")
 public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
 
-    @Autowired
-    private WalletDetailService walletDetailService;
-
     @Override
     public Account getAccountByUserId(Long userId) {
         return lambdaQuery().eq(Account::getUserId, userId).one();

+ 1 - 2
service/src/main/java/com/kym/service/miniapp/impl/AttachmentServiceImpl.java

@@ -10,7 +10,6 @@ import com.kym.common.utils.OssUtil;
 import com.kym.entity.miniapp.Attachment;
 import com.kym.mapper.miniapp.AttachmentMapper;
 import com.kym.service.miniapp.AttachmentService;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
@@ -79,7 +78,7 @@ public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachm
         StpUtil.checkLogin();
         Long userId = CommUtil.null2Long(StpUtil.getSession().get("userId"));
         Attachment attachment = baseMapper.selectOne(Wrappers.query(Attachment.class).eq("uuid", fileId));
-        CommUtil.asserts(null!=attachment&& Objects.equals(attachment.getUploadBy(), userId),"无删除权限");
+        CommUtil.asserts(null != attachment && Objects.equals(attachment.getUploadBy(), userId), "无删除权限");
         String applicationPath = System.getProperty("user.dir");
 
         File dir = new File(applicationPath, fileStoragePath);

+ 3 - 2
service/src/main/java/com/kym/service/miniapp/impl/ChargeServiceImpl.java

@@ -39,7 +39,8 @@ public class ChargeServiceImpl implements ChargeService {
 
     private final EnPlusConfig enPlusConfig;
 
-    public ChargeServiceImpl(EquipmentRelationService equipmentRelationService, ChargeOrderService chargeOrderService, AccountService accountService, EnPlusService enPlusService, EnPlusConfig enPlusConfig) {
+    public ChargeServiceImpl(EquipmentRelationService equipmentRelationService, ChargeOrderService chargeOrderService,
+                             AccountService accountService, EnPlusService enPlusService, EnPlusConfig enPlusConfig) {
         this.equipmentRelationService = equipmentRelationService;
         this.chargeOrderService = chargeOrderService;
         this.accountService = accountService;
@@ -150,7 +151,7 @@ public class ChargeServiceImpl implements ChargeService {
             return chargeOrder;
         } else {
             LOGGER.debug("用户:{}无进行中的订单", userId);
-            throw new BusinessException(ResponseEnum.NO_ORDER_IN_PROGRESS);
+            return null;
         }
     }
 

+ 5 - 3
service/src/main/java/com/kym/service/miniapp/impl/CollectServiceImpl.java

@@ -8,7 +8,6 @@ import com.kym.entity.miniapp.Collect;
 import com.kym.mapper.miniapp.CollectMapper;
 import com.kym.service.admin.StationService;
 import com.kym.service.miniapp.CollectService;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -26,8 +25,11 @@ import java.util.List;
 @DS("db-miniapp")
 public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
 
-    @Autowired
-    private StationService stationService;
+    private final StationService stationService;
+
+    public CollectServiceImpl(StationService stationService) {
+        this.stationService = stationService;
+    }
 
     /**
      * 添加/取消收藏