1. 程式人生 > >spring aop攔截自定義註解的切入點表示式

spring aop攔截自定義註解的切入點表示式

@within(com.cxh.study.aop.controller.UserAccessAnnotation)
表示攔截含有com.cxh.study.aop.controller.UserAccessAnnotation這個註解的類中所有方法

@annotation(com.cxh.study.aop.controller.UserAccessAnnotation)
表示攔截含有這個註解的方法

注意:如果在方法引數上需要該註解,則在表示式中不寫類名,而是寫引數名; 如果攔截的方法上有註解,則可以直接在方法上加入引數就可以取得,如果註解在Class上則需要使用ProceedingJoinPoint通過反射取得註解

示例

package com.cxh.study.aop.controller;

import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import
org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; /** * Created by Jackie on 2015/4/20. * Email : [email protected] */ @Aspect @Component public class LoginInterceptor { /** * A Join Point is defined in the action layer where the method needs * a permission check. */
@Autowired private HttpServletRequest httpServletRequest; @Pointcut("@annotation(com.cxh.study.aop.controller.UserAccessAnnotation)") public void userAccess() {} /** * 這裡需要取得userAccessAnnotation, * 如果註解是在方法上,可以得到,如果註解在類上則為null * @param proceedingJoinPoint * @param userAccessAnnotation * @throws Throwable */ @Around(value = "@within(userAccessAnnotation) || " + "@annotation(userAccessAnnotation)") public void beforeMethod(ProceedingJoinPoint proceedingJoinPoint, UserAccessAnnotation userAccessAnnotation) throws Throwable{ System.out.println("method starts"); proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs()); proceedingJoinPoint.getTarget().getClass(); } /** * 這裡不需要取得userAccessAnnotation * @param proceedingJoinPoint * @throws Throwable */ @Around(value = "@within(com.cxh.study.aop.controller.UserAccessAnnotation) || " + "@annotation(com.cxh.study.aop.controller.UserAccessAnnotation)") public void beforeMethod1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("method starts"); proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs()); proceedingJoinPoint.getTarget().getClass(); } @AfterReturning("userAccess()") public void afterMethod(){ System.out.println("method ends"); } @Before(value = "@within(com.cxh.study.aop.controller.UserAccessAnnotation) || @annotation(com.cxh.study.aop.controller.UserAccessAnnotation)") public void before(JoinPoint joinPoint) throws Throwable { LogFactory.getLog(this.getClass()).info("monitor.before, class: " + joinPoint.getSignature().getDeclaringType().getSimpleName() + ", method: " + joinPoint.getSignature().getName()); } @After(value = "@within(com.cxh.study.aop.controller.UserAccessAnnotation) || @annotation(com.cxh.study.aop.controller.UserAccessAnnotation)") public void after(JoinPoint joinPoint) throws Throwable { LogFactory.getLog(this.getClass()).info("monitor.after, class: " + joinPoint.getSignature().getDeclaringType().getSimpleName() + ", method: " + joinPoint.getSignature().getName()); } }

相關推薦

spring aop攔截定義註解切入點表示式

@within(com.cxh.study.aop.controller.UserAccessAnnotation) 表示攔截含有com.cxh.study.aop.controller.UserAccessAnnotation這個註解的類中所有方法 @an

AOP攔截定義註解並獲取註解屬性與上下文引數(基於Springboot框架)

目錄 AOP可以用於日誌的設計,這樣話就少不了要獲取上下文的資訊,博主在設計日誌模組時考慮了一下此法,整理了一下如何用AOP來攔截你自定義的註解。 自定義註解 首先先自定義一個註解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNT

Spring AOP 結合定義註解的使用

for img support bsp parser disco get asp src 例如要實現一個評論內容的攔截 1,聲明自定義註解,這裏的key()為要攔截的方法中的方法體對應的變量名,可以參考第3點. 2,創建一個切面類,@annatation()中的co

spring boot通過定義註解AOP攔截指定的請求

本文主要通過切面類和自定註解的方式,攔截指定的介面(程式碼中已經作了詳細的說明) 目錄 一 準備工作 三 切面類 五 測試結果  一 準備工作 1.1 新增依賴 通過spr

Spring Boot系列——AOP定義註解的最佳實踐

AOP(Aspect Oriented Programming),即面向切面程式設計,是Spring框架的大殺器之一。 首先,我宣告下,我不是來系統介紹什麼是AOP,更不是照本宣科講解什麼是連線點、切面、通知和切入點這些讓人頭皮發麻的概念。 今天就來說說AOP的一些應用場景以及如何通過和其他特性的結合提升

使用Spring AOP結合定義Java註解實現動態資料來源設定

1、定義Java註解 @Retention(RetentionPolicy.RUNTIME) // 註解將要寫到型別(Class/Interface)還是其它元素(Method等)上,支援package、type、method、field等,一般只會配置一個@Target

使用Spring Boot的AOP處理定義註解

前言 上一篇文章Java 註解介紹講解了下Java註解的基本使用方式,並且通過自定義註解實現了一個簡單的測試工具;本篇文章將介紹如何使用Spring Boot的AOP來簡化處理自定義註解,並將通過實現一個簡單的方法執行時間統計工具為樣例來講解這些內容。

Spring Boot中定義註解+AOP實現主備庫切換

摘要: 本篇文章的場景是做排程中心和監控中心時的需求,後端使用TDDL實現分表分庫,需求:實現關鍵業務的查詢監控,當用Mybatis查詢資料時需要從主庫切換到備庫或者直接連到備庫上查詢,從而減小主庫的壓力,在本篇文章中主要記錄在Spring Boot中通過自定義註解結合AOP實現直接連線備庫查詢。 一.通過A

spring mvc實現定義註解

poi org param 運行時 onf dha ogg logs exec 實現方式:使用@Aspect實現: 1. 新建註解接口:CheckSign package com.soeasy.web.utils; import org.springframework.

spring Aspect 實現定義註解的日誌記錄,有時候註解類不起作用的原因分析

使用只要在controller的method上加上@ActionLog(actionGroup = "freeorder",actionType = "update",actionDesc = "操作",insertDb = true)其中insertDb 代表是否插入資料

spring資料驗證----定義註解

<dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator&l

利用AOP定義註解進行Token校驗

因為公司業務需要每個訪問的方法校驗token,程式碼比較重複,所以就考慮利用Spring的AOP對每個方法進行token校驗。運用到了aop和自定義註解的知識。aop配置檔案 <!-- 自定義註解 --> <bean id="authTok

AOP定義註解實現redis註解快取

  很早之前學習了redis,在這次畢業設計中使用redis對熱點資料進行快取,其中使用spring-data-redis還有@Cacheable、@CachePut和@CacheEvict這3個註解。註解的方式避免了在業務方法中參雜太多非業務的邏輯程式碼,比如日誌和事務,

SpringMVC利用攔截攔截定義註解

前幾篇文章裡寫了關於token的個人思考,那麼具體在程式碼中要怎麼實現攔截token,今天寫了一段程式碼,記錄一下 框架:SpringMVC Spring 執行環境:tomcat8 說一下思路: 1.自定一個註解,然後註解在我的Controller方法上 2.實現Sprin

Aspectj攔截定義註解方法

AspectJ是一個面向切面的框架,具體的解釋百度很詳細.這裡是攔截自定義註解,話不多說, 直接上程式碼: 需要的依賴: <dependency><groupId>org.aspectj</groupId><artifactId&g

SpringBoot框架:通過AOP定義註解完成druid連線池的動態資料來源切換(三)

一、引入依賴   引入資料庫連線池的依賴——druid和麵向切面程式設計的依賴——aop,如下所示: <!-- druid --> <dependency> <groupId

Spring】每個程式設計師都使用Spring(四)——Aop+定義註解做日誌攔截

一、前言       上一篇部落格向大家介紹了Aop的概念,對切面=切點+通知 、連線點、織入、目標物件、代理(jdk動態代理和CGLIB代理)有所瞭解了。理論很強,實用就在這篇部落格介紹。       這篇部落格中,小編向大家介紹springAop很常見的

Spring AOP攔截Service實現日誌管理(定義註解的方式)

最近專案中用到AOP方式進行Service操作方法日誌管理,特為之記! 1、先說理論和採用的方法 採用註解的方式,其中包括以下註解:@Aspect(類註解)和@AfterReturning(方法註解),其中需要用的Maven庫如下: "org.aspectj:aspect

使用Spring註解AOP(基於定義註解和包下攔截方法)

一、基於自定義註解@Pointcut方式 1.自定義一個註解 package spring4.java.service.aop; import java.lang.annotation.Documented; import java.lang.annota

使用Spring AOP定義註解方式實現使用者操作日誌記錄

1,開發環境 作業系統:Windows 7 JDK:1.8.0_161 Eclipse:Mars.2 Release (4.5.2) 2,自定義註解類UserLog @Target({ElementType.PARAMETER, ElementType.METHOD}) @R