如何解決SpringBoot 加入AOP后無(wú)法注入的問(wèn)題
提示錯(cuò)誤
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代理,默認(rèn)為falsespring.aop.proxy-target-class=true
引以為戒啊!!!!!!!
springboot使用aop攔截controller干一些事導(dǎo)致service們@Autowired全部注入失敗springboot使用aop攔截controller干一些事導(dǎo)致controller里的service們@Autowired全部注入失敗,報(bào)空指針
先集成使用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屬性值決定是基于接口的還是基于類的代理被創(chuàng)建。如果proxy-target-class 屬性值被設(shè)置為true,那么基于類的代理將起作用(這時(shí)需要cglib庫(kù))。
如果proxy-target-class屬值被設(shè)置為false或者這個(gè)屬性被省略,那么標(biāo)準(zhǔn)的JDK 基于接口的代理將起作用。
然后直接貼一個(gè)模型代碼吧
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; } /** * 請(qǐng)求參數(shù)拼裝 * @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之后發(fā)現(xiàn) service都注入失敗
解決方案這個(gè)aop只能適用于 protect 和public
之后把controller中的所有方法都改成public
一個(gè)小坑吧~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. 如何在jsp界面中插入圖片4. 爬取今日頭條Ajax請(qǐng)求5. jsp EL表達(dá)式詳解6. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)7. jsp中sitemesh修改tagRule技術(shù)分享8. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼9. ASP基礎(chǔ)知識(shí)VBScript基本元素講解10. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
