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

Spring中基于xml的AOP實(shí)現(xiàn)詳解

 更新時(shí)間:2023年09月13日 09:16:40   作者:蕾峰  
這篇文章主要介紹了Spring中基于xml的AOP實(shí)現(xiàn)詳解,基于xml與基于注解的AOP本質(zhì)上是非常相似的,都是需要封裝橫切關(guān)注點(diǎn),封裝到切面中,然后把橫切關(guān)注點(diǎn)封裝為一個(gè)方法,再把該方法設(shè)置為當(dāng)前的一個(gè)通知,再通過(guò)切入點(diǎn)表達(dá)式定位到橫切點(diǎn)就可以了,需要的朋友可以參考下

基于xml的AOP實(shí)現(xiàn)

基于xml與基于注解的AOP本質(zhì)上是非常相似的,都是需要封裝橫切關(guān)注點(diǎn),封裝到切面中,然后把橫切關(guān)注點(diǎn)封裝為一個(gè)方法,再把該方法設(shè)置為當(dāng)前的一個(gè)通知,再通過(guò)切入點(diǎn)表達(dá)式定位到橫切點(diǎn)就可以了,思路是非常相似的。

我們的接口代碼如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.springframework.stereotype.Component;
@Component
public interface Calculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}

我們的實(shí)現(xiàn)類(lèi)如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.springframework.stereotype.Component;
/**
 *
 */
@Component
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        int result=i+j;
        System.out.println("方法內(nèi)部,result:"+result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result=i-j;
        System.out.println("方法內(nèi)部,result:"+result);
        return  result;
    }
    @Override
    public int mul(int i, int j) {
       int result=i*j;
       System.out.println("方法內(nèi)部,result:"+result);
       return  result;
    }
    @Override
    public int div(int i, int j) {
      int result=i/j;
      System.out.println("方法內(nèi)部,result:"+result);
      return  result;
    }
}

我們封裝的切面方法如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class LoggerAspect {
     public  void  beforeAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",參數(shù):"+ Arrays.toString(args));
    }
    public  void afterAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",執(zhí)行完畢");
    }
    public  void afterReturningAdviceMethod(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",結(jié)果:"+result);
    }
    public  void afterThrowingAdviceMethod(JoinPoint joinPoint,Throwable ex){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",異常:"+ex);
    }
    public  Object  aroundAdviceMethod(ProceedingJoinPoint joinPoint){
       Object result=null;
        try {
            System.out.println("環(huán)繞通知-->前置通知");
            //表示目標(biāo)對(duì)象方法的執(zhí)行
            result = joinPoint.proceed();
            System.out.println("環(huán)繞通知-->返回通知");
        } catch (Throwable throwable) {
           throwable.printStackTrace();
            System.out.println("環(huán)繞通知-->異常通知");
        }finally {
            System.out.println("環(huán)繞通知-->后置通知");
        }
      return result;
    }
}

我們封裝的切面如下所示:

package com.rgf.spring.aop.annotation.xml;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * 切面的優(yōu)先級(jí):
 * 可以通過(guò)@Order注解的value屬性設(shè)置優(yōu)先級(jí),默認(rèn)值Integer的最大值
 * @Order注解的value屬性值越小,優(yōu)先級(jí)越高
 *
 */
@Component
public class ValidateAspect {
    public void beforeMethod(){
        System.out.println("ValidateAspect-->前置通知");
    }
}

我們的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描組件-->
    <context:component-scan base-package="com.rgf.spring.aop.annotation.xml"></context:component-scan>
   <!-- <cop:aspectj-autoproxy:開(kāi)啟基于注解的aop-->
    <aop:config>
        <!--設(shè)置一個(gè)公共的切入點(diǎn)表達(dá)式-->
        <aop:pointcut id="pointcut" expression="execution(* com.rgf.spring.aop.annotation.xml.CalculatorImpl.*(..))"/>
        <!--aop:aspect:將IOC容器中的某個(gè)bean(某個(gè)組件)設(shè)置為切面,通過(guò)ref引用某一個(gè)besn的id,就可以將當(dāng)前這一個(gè)bean來(lái)設(shè)置為一個(gè)切面-->
        <!--aop:advisor:設(shè)置當(dāng)前的通知-->
        <!--aop:pointcut:設(shè)置切入點(diǎn)表達(dá)式-->
        <aop:aspect ref="loggerAspect">
            <aop:before method="beforeAdviceMethod" pointcut-ref="pointcut"></aop:before>
            <aop:after  method="afterAdviceMethod" pointcut-ref="pointcut"></aop:after>
            <aop:after-returning method="afterReturningAdviceMethod" returning="result" pointcut-ref="pointcut"></aop:after-returning>
            <aop:after-throwing method="afterThrowingAdviceMethod" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
            <aop:around method="aroundAdviceMethod" pointcut-ref="pointcut"></aop:around>
        </aop:aspect>
        <!--order進(jìn)行設(shè)置他的優(yōu)先級(jí).設(shè)置的值越小,優(yōu)先級(jí)越高-->
        <aop:aspect ref="validateAspect" order="1">
            <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

我們進(jìn)行測(cè)試如下所示:

package com.rgf.spring.test;
import com.rgf.spring.aop.annotation.xml.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPByXMLTest {
    @Test
    public  void  testAOPByXML(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-xml.xml");
        Calculator calculator = ioc.getBean(Calculator.class);
        calculator.add(1,1);
    }
}

我們進(jìn)行運(yùn)行之后如下所示:

到此這篇關(guān)于Spring中基于xml的AOP實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)基于xml的AOP實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例

    SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例

    這篇文章主要介紹了SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • SpringBoot動(dòng)態(tài)Feign服務(wù)調(diào)用詳解

    SpringBoot動(dòng)態(tài)Feign服務(wù)調(diào)用詳解

    Feign是Netflix公司開(kāi)發(fā)的一個(gè)聲明式的REST調(diào)用客戶端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開(kāi)發(fā)非常基礎(chǔ)的組件,在使用的過(guò)程中我們也發(fā)現(xiàn)它們一般都是同時(shí)出現(xiàn)的,而且配置也都非常相似
    2022-12-12
  • 詳解Java實(shí)現(xiàn)多線程的三種方式

    詳解Java實(shí)現(xiàn)多線程的三種方式

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多線程的三種方式,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Mybatis核心配置文件加載流程詳解

    Mybatis核心配置文件加載流程詳解

    本文將介紹MyBatis在配置文件加載的過(guò)程中,如何加載核心配置文件、如何解析映射文件中的SQL語(yǔ)句以及每條SQL語(yǔ)句如何與映射接口的方法進(jìn)行關(guān)聯(lián),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 解決MyBatis-Plus使用動(dòng)態(tài)表名selectPage不生效的問(wèn)題

    解決MyBatis-Plus使用動(dòng)態(tài)表名selectPage不生效的問(wèn)題

    這篇文章主要介紹了如惡化解決MyBatis-Plus使用動(dòng)態(tài)表名selectPage不生效的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • spring-boot-maven-plugin:unknown的完美解決方法

    spring-boot-maven-plugin:unknown的完美解決方法

    這篇文章主要介紹了spring-boot-maven-plugin:unknown的完美解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java?KeyGenerator.generateKey的19個(gè)方法代碼示例

    Java?KeyGenerator.generateKey的19個(gè)方法代碼示例

    在下文中一共展示了KeyGenerator.generateKey方法的19個(gè)代碼示例,這些例子默認(rèn)根據(jù)受歡迎程度排序
    2021-12-12
  • 一篇文章帶你入門(mén)Java之編程規(guī)范

    一篇文章帶你入門(mén)Java之編程規(guī)范

    這篇文章主要介紹了如何養(yǎng)成良好java代碼編碼規(guī)范,規(guī)范需要平時(shí)編碼過(guò)程中注意,是一個(gè)慢慢養(yǎng)成的好習(xí)慣,下面小編就帶大家來(lái)一起詳細(xì)了解一下吧
    2021-08-08
  • 深度分析java dump文件

    深度分析java dump文件

    java內(nèi)存dump是jvm運(yùn)行時(shí)內(nèi)存的一份快照,利用它可以分析是否存在內(nèi)存浪費(fèi),可以檢查內(nèi)存管理是否合理,當(dāng)發(fā)生OOM的時(shí)候,可以找出問(wèn)題的原因。那么dump文件的內(nèi)容是什么樣的呢?
    2021-05-05
  • java 操作gis geometry類(lèi)型數(shù)據(jù)方式

    java 操作gis geometry類(lèi)型數(shù)據(jù)方式

    這篇文章主要介紹了java 操作gis geometry類(lèi)型數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論

辽宁省| 罗山县| 哈巴河县| 桓台县| 米脂县| 山丹县| 青川县| 杂多县| 韶关市| 鹰潭市| 莎车县| 鹤山市| 浠水县| 洪泽县| 吉木乃县| 平南县| 铜鼓县| 红桥区| 临沭县| 太谷县| 株洲市| 永清县| 镇原县| 惠安县| 府谷县| 德清县| 军事| 南川市| 博客| 靖宇县| 辉县市| 崇左市| 专栏| 乌兰浩特市| 汕头市| 桑日县| 阿合奇县| 会宁县| 新余市| 铜山县| 汶川县|