java/springboot自定義注解實現AOP


 

java注解

即是注釋了,百度解釋:也叫元數據。一種代碼級別的說明。 個人理解:就是內容可以被代碼理解的注釋,一般是一個類。

元數據

也叫元注解,是放在被定義的一個注解類的前面 ,是對注解一種限制。

談下這兩個: @Retention 和 @Target  

@Retention :用來說明該注解類的生命周期。它有以下三個參數:

RetentionPolicy.SOURCE  : 注解只保留在源文件中

RetentionPolicy.CLASS  : 注解保留在class文件中,在加載到JVM虛擬機時丟棄

RetentionPolicy.RUNTIME  : 注解保留在程序運行期間,此時可以通過反射獲得定義在某個類上的所有注解。

@Target :  用來說明該注解可以被聲明在那些元素之前。

ElementType.TYPE:說明該注解只能被聲明在一個類前。

ElementType.FIELD:說明該注解只能被聲明在一個類的字段前。

ElementType.METHOD:說明該注解只能被聲明在一個類的方法前。

ElementType.PARAMETER:說明該注解只能被聲明在一個方法參數前。

ElementType.CONSTRUCTOR:說明該注解只能聲明在一個類的構造方法前。

ElementType.LOCAL_VARIABLE:說明該注解只能聲明在一個局部變量前。

ElementType.ANNOTATION_TYPE:說明該注解只能聲明在一個注解類型前。

ElementType.PACKAGE:說明該注解只能聲明在一個包名前。

 

實現自定義注解AOP:

LogAnnotation.java

package com.pupeiyuan.aop; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.persistence.Inheritance; @Documented//說明該注解將被包含在javadoc中
@Retention(RetentionPolicy.RUNTIME)// 注解會在class字節碼文件中存在,在運行時可以通過反射獲取到
@Target(ElementType.METHOD) @Inheritance//說明子類可以繼承父類中的該注解
public @interface LogAnnotation { String value() default "-----AOP攔截執行完畢!----"; }

 

WebLogAspect.java

package com.pupeiyuan.aop; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @Aspect @Component public class WebLogAspect { protected static final Logger logger = Logger.getLogger(WebLogAspect.class); //定義一個切入點
    @Pointcut("@annotation(com.pupeiyuan.aop.LogAnnotation)") public void annotationPointCut(){ } @Before("annotationPointCut()") public void doBefore(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature(); Method method = methodSignature.getMethod(); LogAnnotation annotation = method.getAnnotation(LogAnnotation.class); // 記錄請求到達時間 //接收到請求,記錄請求內容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 記錄下請求內容
        logger.info("URI : " + request.getRequestURI()); logger.info("URL : " + request.getRequestURL()); logger.info("HTTP_METHOD : " + request.getMethod()); logger.info("IP : " + request.getRemoteAddr()); logger.info(annotation.value()); } }

 

controller

package com.pupeiyuan.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.github.pagehelper.PageHelper; import com.pupeiyuan.aop.LogAnnotation; import com.pupeiyuan.bean.NhReportStatusHistory; import com.pupeiyuan.common.controller.BaseController; import com.pupeiyuan.core.DataSourceKey; import com.pupeiyuan.core.DynamicDataSourceContextHolder; import com.pupeiyuan.services.NhReportService; /** * @author pypua * @date 2018年8月30日 上午9:21:20 * */ @Controller @RequestMapping("burket") @Scope("prototype") public class BurketController extends BaseController { //services層注入
 @Autowired NhReportService nhReportService; @LogAnnotation @RequestMapping(value = "/burketList", method = {RequestMethod.GET,RequestMethod.POST}) public ModelAndView burketList(HttpServletRequest request, HttpServletResponse response ) throws Exception { System.out.println("hello,springboot"); //參數容器
        Map<String, Object> params = new HashMap<String, Object>(); PageHelper.startPage(1, 2); DynamicDataSourceContextHolder.set(DataSourceKey.DB_SLAVE1); List<NhReportStatusHistory> findList = nhReportService.findList(params); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("burketList"); modelAndView.addObject("list", findList); return modelAndView; } }

 

效果

spring攔截器----方法執行之前---------
2018-12-07 16:48:53.769 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : URI : /burket/burketList
2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : URL : http://localhost:8082/burket/burketList
2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : HTTP_METHOD : GET
2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : IP : 0:0:0:0:0:0:0:1
2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : -----AOP攔截執行完畢!----
hello,springboot
2018-12-07 16:48:53.788 INFO 92292 --- [nio-8082-exec-4] c.p.core.DynamicRoutingDataSource : 當前數據源:{}DB_SLAVE1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM