如何解決SpringBoot 加入AOP后無法注入的問題
提示錯誤
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type
或者
java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to cn.edu.nuc.SpringTest.service.impl.DemoServiceImpl
解決方案在application.properties中添加配置
#true為使用CGLIB代理,false為JDK代理,默認為falsespring.aop.proxy-target-class=true
引以為戒啊!!!!!!!
springboot使用aop攔截controller干一些事導致service們@Autowired全部注入失敗springboot使用aop攔截controller干一些事導致controller里的service們@Autowired全部注入失敗,報空指針
先集成使用aop吧
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
properties修改
#aopspring.aop.proxy-target-class=truespring.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@Configurationpublic 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
一個小坑吧~
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. pip已經安裝好第三方庫但pycharm中import時還是標紅的解決方案2. 關于Mysql-connector-java驅動版本問題總結3. CSS自定義滾動條樣式案例詳解4. 詳解CSS偽元素的妙用單標簽之美5. 將properties文件的配置設置為整個Web應用的全局變量實現方法6. Ajax實現表格中信息不刷新頁面進行更新數據7. HTML <!DOCTYPE> 標簽8. SpringBoot+Shiro+LayUI權限管理系統項目源碼9. ajax post下載flask文件流以及中文文件名問題10. msxml3.dll 錯誤 800c0019 系統錯誤:-2146697191解決方法
