debug时Controller内注入的Service为null

debug时Controller内注入的Service为null

Controller内注入的Service为null

问题原因

容器扫描bean生成代理类的时候,public和protected方法可以被正常代理,而private方法的不会被代理,属性的注入也是在代理类中完成,所以public/protected方法获取的注入属性是完成注入的属性,private方法获取的是未完成注入时的属性,所以是null

出错位置
Controller中含有私有方法,导致无法被Spring容器进行相关bean的管理,错误代码如下:

/**
     * 获取预约数据封装成的Z-Tree树数据
     * @return
     */
    @RequestMapping("/getCatesTree")
    @ResponseBody
    private R getCatesTree(@RequestParam String catePId) {
    
    
        //设定访问调用远程的api接口url
        String api = apiUrl + "?catePId="+ catePId;
        String result = ApiCateUtils.httpRequest(api);
        System.out.println(result);
        JSONObject jsonObject = JSON.parseObject(result);
        String code = jsonObject.getString("code");
        if ("0".equals(code)) {
    
    
            String data = jsonObject.getString("data");
            System.out.println(data);
            return R.ok().put("data",data);
        }
        return R.error("获取数据失败");
    }

解决方案

将Controller类含有的接口访问方法修正为public方法。

以上为采矿Controller内注入的Service为null的教训,望诸君共勉。

猜你喜欢

转载自blog.csdn.net/rao991207823/article/details/106336571
今日推荐