欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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);
}

我們的實現(xiàn)類如下所示:

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)對象方法的執(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)先級:
 * 可以通過@Order注解的value屬性設(shè)置優(yōu)先級,默認(rèn)值Integer的最大值
 * @Order注解的value屬性值越小,優(yōu)先級越高
 *
 */
@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:開啟基于注解的aop-->
    <aop:config>
        <!--設(shè)置一個公共的切入點表達(dá)式-->
        <aop:pointcut id="pointcut" expression="execution(* com.rgf.spring.aop.annotation.xml.CalculatorImpl.*(..))"/>
        <!--aop:aspect:將IOC容器中的某個bean(某個組件)設(shè)置為切面,通過ref引用某一個besn的id,就可以將當(dāng)前這一個bean來設(shè)置為一個切面-->
        <!--aop:advisor:設(shè)置當(dāng)前的通知-->
        <!--aop:pointcut:設(shè)置切入點表達(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)先級.設(shè)置的值越小,優(yōu)先級越高-->
        <aop:aspect ref="validateAspect" order="1">
            <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

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

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實現(xiàn)詳解的文章就介紹到這了,更多相關(guān)基于xml的AOP實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java用數(shù)組實現(xiàn)循環(huán)隊列的示例

    Java用數(shù)組實現(xiàn)循環(huán)隊列的示例

    下面小編就為大家?guī)硪黄狫ava用數(shù)組實現(xiàn)循環(huán)隊列的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • springboot排除某些自動配置的操作方法

    springboot排除某些自動配置的操作方法

    Spring Boot 提供的自動配置非常強(qiáng)大,某些情況下,自動配置的功能可能不符合我們的需求,需要我們自定義配置,這個時候就需要排除/禁用Spring Boot 某些類的自動化配置了,本文給大家介紹springboot排除某些自動配置的方法,感興趣的朋友一起看看吧
    2023-08-08
  • Spring-Boot 集成Solr客戶端的詳細(xì)步驟

    Spring-Boot 集成Solr客戶端的詳細(xì)步驟

    本篇文章主要介紹了Spring-Boot 集成Solr客戶端的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問題

    解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問題

    下面小編就為大家?guī)硪黄鉀Qspring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 基于SpringBoot生成二維碼的幾種實現(xiàn)方式

    基于SpringBoot生成二維碼的幾種實現(xiàn)方式

    本文將基于Spring Boot介紹兩種生成二維碼的實現(xiàn)方式,一種是基于Google開發(fā)工具包,另一種是基于Hutool來實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2022-03-03
  • SpringSecurity 自定義認(rèn)證登錄的項目實踐

    SpringSecurity 自定義認(rèn)證登錄的項目實踐

    本文主要介紹了SpringSecurity 自定義認(rèn)證登錄的項目實踐,以手機(jī)驗證碼登錄為例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Maven jar包下載失敗問題的處理方法

    Maven jar包下載失敗問題的處理方法

    很多同學(xué)在Maven里下載一些依賴的時候,即下載jar包的時候總是會出現(xiàn)一些問題,本文將就這個問題給大家詳細(xì)的講解一下,需要的朋友可以參考下
    2023-06-06
  • SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改

    SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改

    本文主要介紹了SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • MyBatisPlus3如何向數(shù)據(jù)庫中存入List

    MyBatisPlus3如何向數(shù)據(jù)庫中存入List

    本文主要介紹了Mybatis Plus的類型處理器的使用,通過User.java和UserMapper.xml示例進(jìn)行詳細(xì)的解析,并提供了JSON解析器的使用方法,希望通過這篇文章,可以幫助大家更好的理解和掌握Mybatis Plus的類型處理器
    2024-10-10
  • Spring MVC 文件上傳下載的實例

    Spring MVC 文件上傳下載的實例

    本篇文章主要介紹了Spring MVC 文件上傳下載的實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論