Spring AOP 靜態普通方法名匹配切面操作

2022-09-27 07:33:11 字數 2673 閱讀 3894

staticmethodmatcherpointcutadvisor代表乙個靜態方法匹配切面,它通過staticmethodmatcherpointcut來定義切點,並通過類過濾和方法名來匹配所定義的切點.

**已託管到github—>

我們假設我們業務類中 waiter和 seller中都有同名的greetto()方法.

package com.xgj.aop.spring.advisor.staticmethodmatcherpointcutadvisor;

public class waiter

/***

* * @title: serverto

* * @description:

* * @param name

* * @return: void

*/ public void serverto(string name)

}package com.xgj.aop.spring.advisor.staticmethodmatcherpointcutadvisor;

public class seller

}現在我們希望通過staticmethodmatcherpointcutadvisor定義乙個切面,在waiter#greetto()方法呼叫前織入乙個增強,即連線點為waiter#greetto()方法呼叫前的位置。

package com.xgj.aop.spring.advisor.staticmethodmatcherpointcutadvisor;

import j**a.lang.reflect.method;

import org.springframework.aop.classfilter;

import org.springframework.aop.support.staticmethodmatcherpointcutadvisor;

/** *

* * @classname: greetingadvisor

* * @description: 切面類

* * @author: mr.yang

* * @date: 2023年8月18日 下午8:27:52

*/public class greetingadvisor extends staticmethodmatcherpointcutadvisor

/*** 預設情況下,匹配所有的類,重寫getclassfilter,定義匹配規則 切點型別匹配規則,為waiter的類或者之類

*/public classfilter getclassfilter()

};}}

staticmethodmatcherpointcutadvisor 抽象類唯一需要定義的是matches()方法,在預設情況下,該切面匹配所有的類,這裡通過覆蓋getclassfilter()方法,讓它僅匹配waiter類及其子類。

當然,advisor還需要乙個增強類的配合 .

package com.xgj.aop.spring.advisor.staticmethodmatcherpointcutadvisor;

import j**a.lang.reflect.method;

import org.springframework.aop.methodbeforeadvice;

/** *

* * @classname: greetbeforeadivce

* * @description: 前置增強

* * @author: mr.yang

* * @date: 2023年8月18日 下午8:27:40

*/public class greetbeforeadivce implements methodbeforeadvice

}<?xml version="1.0" encoding="utf-8"?>

程式設計客棧.xsd">

程式設計客棧interceptornames="greetadvicesor"

p:proxytargetclass="true"/>

package com.xgj.aop.spring.advisor.staticmethodmatcherpointcutadvisor;

import org.junit.test;

import org.springframework.context.applicationcontext;

import org.springframework.context.support.classpathxmlapplicationcontext;

/** *

* * @classname: staticmethodmatcherpointcutadvisortest

* * @description: 測試類

* * @author: mr.yang

* * @date: 2023年8月18日 下午8:29:28

*/public class staticmethodmatcherpointcutadvisortest

}執行結果:

我們可以看到切面僅僅織入了wwaiter.greetto()方法呼叫前的連線點上, waiter.serverto()和seller.greetto()方法並沒有織入切面。

本文標題: spring-aop 靜態普通方法名匹配切面操作

本文位址: /ruanjian/j**a/409353.html

Python普通方法 靜態方法 類方法

coding utf 8 普通方法,類方法,靜態方法的區別 metaclass type class tst name tst data this is data 普通方法 defnormalmethod self,name print self.data,name 類方法,可以訪問類屬性 clas...

Python普通方法 靜態方法 類方法

普通方法,類方法,靜態方法的區別 metaclass type class tst name tst data this is data 普通方法 def normalmethod self,name print self.data,name 類方法,可以訪問類屬性 classmethod def ...

Python普通方法 靜態方法 類方法

coding utf 8 普通方法,類方法,靜態方法的區別 metaclass type class tst name tst data this is data 普通方法 defnormalmethod self,name print self.data,name 類方法,可以訪問類屬性 clas...