最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Spring AOP之PointCut案例詳解

 更新時(shí)間:2021年09月03日 08:26:56   作者:敲代碼的小小酥  
這篇文章主要介紹了Java Spring AOP之PointCut案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

一、PointCut接口

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop;

/**
 * Core Spring pointcut abstraction.
 *
 * <p>A pointcut is composed of a {@link ClassFilter} and a {@link MethodMatcher}.
 * Both these basic terms and a Pointcut itself can be combined to build up combinations
 * (e.g. through {@link org.springframework.aop.support.ComposablePointcut}).
 *
 * @author Rod Johnson
 * @see ClassFilter
 * @see MethodMatcher
 * @see org.springframework.aop.support.Pointcuts
 * @see org.springframework.aop.support.ClassFilters
 * @see org.springframework.aop.support.MethodMatchers
 */
public interface Pointcut {

	/**
	 * Return the ClassFilter for this pointcut.
	 * @return the ClassFilter (never {@code null})
	 */
	ClassFilter getClassFilter();

	/**
	 * Return the MethodMatcher for this pointcut.
	 * @return the MethodMatcher (never {@code null})
	 */
	MethodMatcher getMethodMatcher();


	/**
	 * Canonical Pointcut instance that always matches.
	 */
	Pointcut TRUE = TruePointcut.INSTANCE;

}

由源碼可知,PointCut接口就是定義了兩個(gè)元素,ClassFilter和MethodMatcher。PointCut接口就是為了獲得這兩個(gè)元素。換句話說,PointCut的功能,都包含在了這兩個(gè)元素里。下面看這 兩個(gè)元素源碼。

二、ClassFilter接口

/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop;

/**
 * Filter that restricts matching of a pointcut or introduction to
 * a given set of target classes.
 *
 * <p>Can be used as part of a {@link Pointcut} or for the entire
 * targeting of an {@link IntroductionAdvisor}.
 *
 * <p>Concrete implementations of this interface typically should provide proper
 * implementations of {@link Object#equals(Object)} and {@link Object#hashCode()}
 * in order to allow the filter to be used in caching scenarios &mdash; for
 * example, in proxies generated by CGLIB.
 *
 * @author Rod Johnson
 * @see Pointcut
 * @see MethodMatcher
 */
@FunctionalInterface
public interface ClassFilter {

	/**
	 * Should the pointcut apply to the given interface or target class?
	 * @param clazz the candidate target class
	 * @return whether the advice should apply to the given target class
	 */
	boolean matches(Class<?> clazz);


	/**
	 * Canonical instance of a ClassFilter that matches all classes.
	 */
	ClassFilter TRUE = TrueClassFilter.INSTANCE;

}

這個(gè)接口用來過濾要生成代理的類和給定的類是否匹配。
該接口的實(shí)現(xiàn)類應(yīng)該提供正確的equals()方法和hashCode()方法,以便于能在緩存中使用。例如通過cglib生成的代理對象。(這句話什么意思,不理解)。
matches方法就是判斷參數(shù)中的class類是否和切點(diǎn)定義的類相匹配。如果匹配,則生成代理對象,如果不匹配,則過濾掉。

三、MethodMatcher接口

/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop;

import java.lang.reflect.Method;

/**
 * Part of a {@link Pointcut}: Checks whether the target method is eligible for advice.
 *
 * <p>A MethodMatcher may be evaluated <b>statically</b> or at <b>runtime</b> (dynamically).
 * Static matching involves method and (possibly) method attributes. Dynamic matching
 * also makes arguments for a particular call available, and any effects of running
 * previous advice applying to the joinpoint.
 *
 * <p>If an implementation returns {@code false} from its {@link #isRuntime()}
 * method, evaluation can be performed statically, and the result will be the same
 * for all invocations of this method, whatever their arguments. This means that
 * if the {@link #isRuntime()} method returns {@code false}, the 3-arg
 * {@link #matches(java.lang.reflect.Method, Class, Object[])} method will never be invoked.
 *
 * <p>If an implementation returns {@code true} from its 2-arg
 * {@link #matches(java.lang.reflect.Method, Class)} method and its {@link #isRuntime()} method
 * returns {@code true}, the 3-arg {@link #matches(java.lang.reflect.Method, Class, Object[])}
 * method will be invoked <i>immediately before each potential execution of the related advice</i>,
 * to decide whether the advice should run. All previous advice, such as earlier interceptors
 * in an interceptor chain, will have run, so any state changes they have produced in
 * parameters or ThreadLocal state will be available at the time of evaluation.
 *
 * <p>Concrete implementations of this interface typically should provide proper
 * implementations of {@link Object#equals(Object)} and {@link Object#hashCode()}
 * in order to allow the matcher to be used in caching scenarios &mdash; for
 * example, in proxies generated by CGLIB.
 *
 * @author Rod Johnson
 * @since 11.11.2003
 * @see Pointcut
 * @see ClassFilter
 */
public interface MethodMatcher {

	/**
	 * Perform static checking whether the given method matches.
	 * <p>If this returns {@code false} or if the {@link #isRuntime()}
	 * method returns {@code false}, no runtime check (i.e. no
	 * {@link #matches(java.lang.reflect.Method, Class, Object[])} call)
	 * will be made.
	 * @param method the candidate method
	 * @param targetClass the target class
	 * @return whether or not this method matches statically
	 */
	boolean matches(Method method, Class<?> targetClass);

	/**
	 * Is this MethodMatcher dynamic, that is, must a final call be made on the
	 * {@link #matches(java.lang.reflect.Method, Class, Object[])} method at
	 * runtime even if the 2-arg matches method returns {@code true}?
	 * <p>Can be invoked when an AOP proxy is created, and need not be invoked
	 * again before each method invocation,
	 * @return whether or not a runtime match via the 3-arg
	 * {@link #matches(java.lang.reflect.Method, Class, Object[])} method
	 * is required if static matching passed
	 */
	boolean isRuntime();

	/**
	 * Check whether there a runtime (dynamic) match for this method,
	 * which must have matched statically.
	 * <p>This method is invoked only if the 2-arg matches method returns
	 * {@code true} for the given method and target class, and if the
	 * {@link #isRuntime()} method returns {@code true}. Invoked
	 * immediately before potential running of the advice, after any
	 * advice earlier in the advice chain has run.
	 * @param method the candidate method
	 * @param targetClass the target class
	 * @param args arguments to the method
	 * @return whether there's a runtime match
	 * @see MethodMatcher#matches(Method, Class)
	 */
	boolean matches(Method method, Class<?> targetClass, Object... args);


	/**
	 * Canonical instance that matches all methods.
	 */
	MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;

}

PointCut的一部分,用來判斷方法是否需要進(jìn)行增強(qiáng)。ClassFilter過濾需要生成代理的類。而這個(gè)類里,不是所有的方法都需要增強(qiáng)的,所以要通過MethodMatcher接口匹配出要增強(qiáng)的方法來。
MethodMatcher分為靜態(tài)匹配和動態(tài)匹配。靜態(tài)匹配是根據(jù)方法名匹配。動態(tài)匹配是根據(jù)參數(shù)進(jìn)行匹配(不知道這么理解對不對)
MethodMatcher的實(shí)現(xiàn)類中,如果isRuntime()方法返回false,則使用靜態(tài)匹配,無論參數(shù)是什么,只要方法名匹配,則都會進(jìn)行增強(qiáng)。而且這個(gè)接口中帶有3個(gè)參數(shù)的matches方法,即matches(java.lang.reflect.Method, Class, Object[])就永遠(yuǎn)不會執(zhí)行。
實(shí)現(xiàn)類中,如果兩個(gè)參數(shù)的matches方法返回true,且isRuntime()也返回true。那么在執(zhí)行增強(qiáng)之前,會執(zhí)行三個(gè)參數(shù)的matches方法,來判斷這個(gè)增強(qiáng)是否要執(zhí)行。

下面來看接口三個(gè)方法的注釋:

boolean matches(Method method, Class<?> targetClass);

靜態(tài)核對給定的方法是否需要增強(qiáng)。如果返回false。則不再會執(zhí)行三個(gè)參數(shù)的matches方法。

boolean isRuntime();

當(dāng)兩個(gè)參數(shù)的matches方法返回true時(shí),是否要執(zhí)行三個(gè)參數(shù)的matches方法。該方法在AOP代理生成的時(shí)候執(zhí)行,而不是每次調(diào)用代理方法之前執(zhí)行。意思就是生成代理對象的時(shí)候,就已經(jīng)判斷好要不要執(zhí)行三個(gè)參數(shù)的matches方法了。

boolean matches(Method method, Class<?> targetClass, Object... args);

該方法執(zhí)行的時(shí)機(jī)是增強(qiáng)方法要執(zhí)行之前,判斷參數(shù)是否滿足要求,如果滿足,則執(zhí)行增強(qiáng)。注意isRuntime方法是創(chuàng)建代理對象時(shí)就決定好的,而是否執(zhí)行三個(gè)參數(shù)的matches方法是調(diào)用增強(qiáng)之前判斷的。

總結(jié)

從上面的源碼可以看出,PointCut就是起到過濾的作用,首先是過濾類,然后再過濾方法,篩選出需要加強(qiáng)的方法來。由接口可知,我們要將參數(shù)中的class或method進(jìn)行比較,然后過濾,那么,和誰進(jìn)行比較呢?要過濾的類和方法的規(guī)則存在哪里了呢?請看下回分解。

到此這篇關(guān)于Java Spring AOP之PointCut案例詳解的文章就介紹到這了,更多相關(guān)Java Spring AOP之PointCut內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot通過配置WebMvcConfig處理Cors非同源訪問跨域問題

    Springboot通過配置WebMvcConfig處理Cors非同源訪問跨域問題

    這篇文章主要介紹了Springboot通過配置WebMvcConfig處理Cors非同源訪問跨域問題,關(guān)于Cors跨域的問題,前端有代理和jsonp的常用方式解決這種非同源的訪問拒絕策略
    2023-04-04
  • 解決mybatis映射mapper.xml文件不編譯的問題

    解決mybatis映射mapper.xml文件不編譯的問題

    這篇文章主要介紹了解決mybatis映射mapper.xml文件不編譯的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • knife4j+springboot3.4異常無法正確展示文檔

    knife4j+springboot3.4異常無法正確展示文檔

    本文主要介紹了knife4j+springboot3.4異常無法正確展示文檔,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • 一文詳解Object類和抽象類

    一文詳解Object類和抽象類

    這篇文章主要介紹了一文詳解Object類和抽象類,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下。希望對你的學(xué)習(xí)有所幫助
    2022-08-08
  • spring監(jiān)視器actuator配置應(yīng)用

    spring監(jiān)視器actuator配置應(yīng)用

    這篇文章主要介紹了spring監(jiān)視器actuator配置應(yīng)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Hibernate中5個(gè)核心接口知識點(diǎn)整理

    Hibernate中5個(gè)核心接口知識點(diǎn)整理

    在本篇文章里小編給大家整理的是一篇關(guān)于Hibernate中5個(gè)核心接口知識點(diǎn)整理等內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)參考下。
    2021-08-08
  • Java concurrency之LockSupport_動力節(jié)點(diǎn)Java學(xué)院整理

    Java concurrency之LockSupport_動力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了Java concurrency之LockSupport的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Java設(shè)計(jì)模式之觀察者模式解析

    Java設(shè)計(jì)模式之觀察者模式解析

    這篇文章主要介紹了Java設(shè)計(jì)模式之觀察者模式解析,觀察者模式,又被稱為發(fā)布/訂閱模式,它定義了一種一對多的依賴關(guān)系,讓多個(gè)觀察者對象同時(shí)監(jiān)聽某一個(gè)主題對象,這個(gè)主題對象在狀態(tài)變化時(shí),會通知所有的觀察者對象,使他們能夠自動更新自己,需要的朋友可以參考下
    2023-09-09
  • 使用Java代碼來比較Android客戶端版本號

    使用Java代碼來比較Android客戶端版本號

    這篇文章主要介紹了使用Java代碼來比較Android客戶端版本號,Java是目前安卓程序唯一的開發(fā)語言,需要的朋友可以參考下
    2015-07-07
  • 淺析Java編程中類和對象的定義

    淺析Java編程中類和對象的定義

    下面小編就為大家?guī)硪黄獪\析Java編程中類和對象的定義。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦
    2016-05-05

最新評論

黄浦区| 砀山县| 什邡市| 毕节市| 阳新县| 保定市| 康定县| 嘉祥县| 微山县| 奎屯市| 永定县| 简阳市| 玛多县| 大冶市| 金沙县| 鄯善县| 大同县| 遂宁市| 靖远县| 通化县| 确山县| 瓮安县| 中卫市| 开化县| 蚌埠市| 年辖:市辖区| 平塘县| 连山| 叶城县| 宜丰县| 大余县| 休宁县| 昌图县| 公主岭市| 灵山县| 台北县| 平凉市| 班玛县| 甘孜县| 淅川县| 宁远县|