Spring4 Aop on Spring mvc controller错误

时间:2016-09-02 11:32:05

标签: spring-mvc aop aspectj

代码:

@Aspect
@Order(Integer.MAX_VALUE)
public class KeySortedAspect {

    @Pointcut(value = "@annotation(com.le.bigdata.convertor.KeySortedRule)")
    public void pointCut(){}

    @Around(value = "pointCut()")
    public Object keySorted(ProceedingJoinPoint joinPoint) throws Throwable {

        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Object result = joinPoint.proceed();
        if(methodSignature.getMethod().isAnnotationPresent(KeySortedRule.class)){
            KeySortedRule keySortedRule = methodSignature.getMethod().getAnnotation(KeySortedRule.class);
            String path = keySortedRule.path();
        //String rule = keySortedRule.rule();
        try {
            Map<String, String[]> keyMap = getRule(path);
            Class<?> type = methodSignature.getMethod().getReturnType();
            Object a = sortedUseKeyList(result, keyMap, type);
            return a;
        } catch (IOException e) {
            logger.error("Key sorted Error");
            logger.error("reason ", e);
        }
        }
        return result;
    }
}

spring mvc configuration mvc-dispatcher-servlet.xml:

<context:annotation-config />
<!-- controller层 内部ctrl与lebi对接的ctrl -->
<context:component-scan base-package="com.le.bigdata.controller, com.le.bigdata.api.controller"/>

<bean id="keySortedAspect" class="com.le.bigdata.convertor.KeySortedAspect"/>

<aop:aspectj-autoproxy proxy-target-class="true"/>

<mvc:default-servlet-handler/>

控制器代码:

@RestController
@RequestMapping("cp")
public class ContentPortraitController {
    @Autowired
    private IContentPortraitService service;

    @RequestMapping(value = "usertop", method = RequestMethod.GET)
    @ApiOperation(value = "获取默认的cv排名前10名的专辑")
    private CommonResponseDTO getUserTop(@RequestParam(defaultValue = "10") int size){
        List<Map<String, Object>> list = service.getDefaultPid(size);
        return new CommonResponseDTO(true, list);
    }

    @RequestMapping(value = "/album", method = RequestMethod.GET)
    @ApiOperation(value = "获取专辑画像", httpMethod = "GET")
    @KeySortedRule(path="content_portrait")
    public JSONObject getAlbum(@RequestParam(value="dt",required = false) String dt,
                           @RequestParam(value="start_dt",required = false) String start,
                           @RequestParam(value="end_dt",required = false) String end,
                           @RequestParam(value="product",required = false) List<String> product,
                           @RequestParam(value="pid") String pid){

        JSONObject result = new JSONObject();
        JSONObject data = null;
        try {
            if(dt != null && !dt.isEmpty()){
            //
                data = service.getAlbumPortrait(pid, dt, product);
            } else if(start != null && end != null && !start.isEmpty() && !end.isEmpty()){
            //
                data = service.getAlbumPortrait(pid, start, end, product);
            }
        } catch (Exception e) {
            return null;
        }
        result.put("result", data);
        return result;
}
}

当我请求/ cp / album它工作和响应数据。 但我请求/ cp / usertop它抛出一个异常:NullPointException 参考服务是空的! service不能为null,IContentPortraitService是一个接口,只有一个实现。我绝对相信我已经扫描了服务包并在它的实现上使用@Service注释。

调试信息:

当我请求/ cp / usertop:

ContentProtrationController实例是ContentPortraitServiceImpl $$ EnhancerBySprinigCGLIB $ bdfd5678 @ 7591且服务为空

当我请求/ cp / album:

ContentProtrationController实例是ContentPortraitServiceImpl @ 7591,服务不是null,它可以工作。

我的代码出了什么问题?这个问题困扰了我很多天。谁知道如何解决这个问题?

0 个答案:

没有答案