springboot使用aop攔截controller幹一些事導致service們@Autowired全部注入失敗
阿新 • • 發佈:2019-02-16
springboot使用aop攔截controller幹一些事導致controller裡的service們@Autowired全部注入失敗,報空指標
先整合使用aop吧
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
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 基於介面的代理將起作用。
**
然後直接貼一個模型程式碼吧
**
package cc.datebook.aop;
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<Long> 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就正常…
一個小坑吧