springboot使用aop拦截controller干一些事导致service们@Autowired全部注入失败

springboot使用aop拦截controller干一些事导致controller里的service们@Autowired全部注入失败,报空指针

先集成使用aop吧


	org.springframework.boot
	spring-boot-starter-aop

properties加点玩意

#aop
spring.aop.proxy-target-class=true
spring.aop.auto=true

proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。
**

然后直接贴一个模型代码吧

**

import cc.datebook.utils.IpUtil;
import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by wangH on 2017/12/12.
 */
@Aspect
@Configuration
public class ControllerMonitorAop {

    private static final Logger logger = LoggerFactory.getLogger(ControllerMonitorAop.class);
    ThreadLocal startTime = new ThreadLocal<>();
    
    @Pointcut("execution(public * cc.datebook.web.*Controller.*(..))")
    public void excudeService() {}

    @Around("excudeService()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        String ipAddr = IpUtil.getIpAddr(request);
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        String params = "";
        if ("POST".equals(method)) {
            Object[] paramsArray = pjp.getArgs();
            params = argsArrayToString(paramsArray);
        } else {
            Map paramsMap = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
            params = paramsMap.toString();
        }
        logger.info("request begin=>ipAddr: {}, url: {}, method: {}, uri: {}, params: {}", ipAddr, url, method, uri, params);

        // result的值就是被拦截方法的返回值
        Object result = pjp.proceed();
        Gson gson = new Gson();
        String ab = gson.toJson(result).toString();
        if (ab.length() > 200){
            ab = ab.substring(0,200);
        }
        logger.info("request end=>" + ab);
        return result;
    }

    /**
     * 请求参数拼装
     * @param paramsArray
     * @return
     */
    private String argsArrayToString(Object[] paramsArray) {
        String params = "";
        if (paramsArray != null && paramsArray.length > 0) {
            for (int i = 0; i < paramsArray.length; i++) {
                Gson gson = new Gson();
                Object jsonObj = gson.toJson(paramsArray[i]);
                params += jsonObj.toString() + " ";
            }
        }
        return params.trim();
    }
}

但是拦截所有controller之后发现 service都注入失败
解决方案 这个aop只能适用于 protect 和public
之后把controller中的所有方法都改成public

springboot使用aop拦截controller干一些事导致service们@Autowired全部注入失败_第1张图片

一个小坑吧

你可能感兴趣的:(aop,springboot)