zoukankan      html  css  js  c++  java
  • new和@Autowired的区别

    1. controller层:

      @RequestMapping("/payment")
      @RestController
      public class WxPayController {
      
          public WxPayServiceImpl wxPayService = new WxPayServiceImpl();
      
          @RequestMapping("/wxpay/{product_id}")
          public String getWxQrCode(@PathVariable("product_id") Integer productId) {
              System.out.println(productId);
              return wxPayService.getWxQrCode(productId);
          }
      }
      

      service层:

      @Service
      public class WxPayServiceImpl implements WxPayService {
      
          @Autowired
          public WxPayConfigBean wxPayConfigBean;
      
          @Override
          public String getWxQrCode(Integer productId) {
              System.out.println("wxPayConfigBean:" + wxPayConfigBean);
              HashMap<String, String> map = new HashMap<String,String>();
              map.put("appid",wxPayConfigBean.getAppID());
              map.put("mch_id",wxPayConfigBean.getMchID());
              //这里获得的是以秒为单位的时间戳
              Long timeStamp =  WXPayUtil.getCurrentTimestamp();
              map.put("time_stamp",timeStamp.toString());
              map.put("product_id",productId.toString());
              try {
                  map.put("sign",WXPayUtil.generateSignature(map,wxPayConfigBean.getKey()));
              } catch (Exception exception) {
                  exception.printStackTrace();
              }
              String str = "//wxpay/bizpayurl?"
                      + "sign=" + map.get("sign")
                      + "&appid=" + map.get("appid")
                      + "&mch_id= " + map.get("mch_id")
                      + "&product_id=" + map.get("product_id")
                      + "&time_stamp=" + map.get("time_stamp")
                      + "&nonce_str=" + map.get("nonce_str");
              return JsonUtil.ajaxReturn(str);
          }
      }
      
      

      测试该接口可以发现:wxPayConfigBean:null,显然即便这里用了@Autowired但还是注入失败了.然是如果我们通过junit直接测试:

      @Autowired
      public WxPayConfigBean wxPayConfigBean;
      
      @Test
      void contextLoads() {
          System.out.println(wxPayConfigBean.toString());
      }
      

      此时,数据注入又是成功的.

    2. 查询了比较多的资料后我突然明白自己对于new和@Autuwired的理解还是有很大问题.new创建了一个新对象,但Autowired不是,它是通过先匹配类型再匹配名字的方式(这个顺序正好和@Resource相反)取出由spring管理的对象.回到上面的代码,可以发现在controller层我们拿到的WxPayServiceImpl对象是new出来的,而不是从spring管理的对象中取出的,那么WxPayServiceImpl中的wxPayConfigBean对象更不可能是spring中配置好的对象(相当于无参构造),所以导致最后输出为Null,只要稍微修改一下即可

      @RequestMapping("/payment")
      @RestController
      public class WxPayController {
      
          @Autowired
          public WxPayServiceImpl wxPayService;
      
          @RequestMapping("/wxpay/{product_id}")
          public String getWxQrCode(@PathVariable("product_id") Integer productId) {
              System.out.println(productId);
              return wxPayService.getWxQrCode(productId);
          }
      }
      
  • 相关阅读:
    (转载)SAPI 包含sphelper.h编译错误解决方案
    C++11标准的智能指针、野指针、内存泄露的理解(日后还会补充,先浅谈自己的理解)
    504. Base 7(LeetCode)
    242. Valid Anagram(LeetCode)
    169. Majority Element(LeetCode)
    100. Same Tree(LeetCode)
    171. Excel Sheet Column Number(LeetCode)
    168. Excel Sheet Column Title(LeetCode)
    122.Best Time to Buy and Sell Stock II(LeetCode)
    404. Sum of Left Leaves(LeetCode)
  • 原文地址:https://www.cnblogs.com/Arno-vc/p/13509676.html
Copyright © 2011-2022 走看看