亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring 使用xml配置AOP的過程詳解

 更新時間:2023年11月24日 10:40:06   作者:比奇堡的天沒有云  
在之前的學習中,都是使用注解的方式進行AOP的配置.其實使用xml配置文件也可以配置AOP,本文給大家分享Spring 使用xml配置AOP的過程,感興趣的朋友一起看看吧

1.前言

在之前的學習中,都是使用注解的方式進行AOP的配置.其實使用xml配置文件也可以配置AOP.

2.xml配置AOP

xml配置AOP方法如下:

添加相關依賴

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.29</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
    </dependency>
</dependencies>    

相關bean放到Spring容器中

@Service
public class StudentService {
    public void insert(){
        System.out.println("StudentService中的insert方法");
    }
}

創(chuàng)建切面類注入到Spring中,我這里使用的是@Component注解,也可以在配置文件中使用Bean標簽

@Component
public class Aspect {
    @Pointcut("execution(* com.example.service..*.*(..)")
    public void pt(){
        System.out.println("");
    }
    public void methodBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        System.out.println("Before");
    }
}

在配置文件中開啟組件掃描(因為我在將相應的Bean注入到Spring中時,使用的是注解,如果使用Bean標簽,這一步可以省略)

<context:component-scan base-package="com.example">
</context:component-scan>

在配置文件中配置AOP,將切面類(StudentService)中的methodBefore方法設置為前置通知

    <aop:config>
        <!--定義切面-->
        <aop:pointcut id="pt" expression="execution(* com.example.service..*.*(..))"/>
        <!--配置切面-->
        <aop:aspect ref="aspect">
            <!--配置通知類型-->
            <!-- <aop:before method="methodBefore" pointcut-ref="pt"/> -->
            <aop:before method="methodBefore" pointcut="com.example.aspect.Aspect.pt()"/>
        </aop:aspect>
    </aop:config>

配置通知類型中有兩種寫法,一種是用pointcut-ref屬性,值是定義切面時的id,另一種是使用pointcut屬性,需要指定切點方法的全類名

運行結果:

在這里插入圖片描述

可以看到成功將StudentService中的methodBefore方法設置為前置通知了

接下來講一下復雜的通知如何配置,如下:

@AfterReturning(value = "point()",returning = "ret")
public void methodAfterReturning(JoinPoint joinPoint, Object ret){
	// 方法體
}
@AfterThrowing(value = "point()",throwing = "e")
public void methodAfterThrowing(JoinPoint joinPoint,Throwable e){
	// 方法體
}

@AfterReturning和@AfterThrowing是有兩個參數(shù)的

以@AfterReturning為例,在切面類中添加對應的普通方法:

@Component
public class Aspect {
    @Pointcut("execution(* com.example.service..*.*(..))")
    public void pt(){
        System.out.println("");
    }
    public void methodBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        System.out.println("Before");
    }    
    public void methodAfterReturning(JoinPoint joinPoint, Object ret){
        System.out.println("AfterReturning: "+ ret);
    }
}
<aop:aspect ref="aspect">
    <!--配置通知類型-->
    <!-- <aop:before method="methodBefore" pointcut-ref="pt"/> -->
    <aop:before method="methodBefore" pointcut="com.example.aspect.Aspect.pt()"/>
    <aop:after-returning method="methodAfterReturning" pointcut-ref="pt" returning="ret"/>
</aop:aspect>

需要注意在設置AOP時,標簽中有returning這樣一個屬性

運行結果:

在這里插入圖片描述

3. 總結

xml配置AOP的重要步驟:

  • 定義切面類,定義切點.
  • 將目標類和切面類添加到Spring容器中(注解或Bean標簽),如果是注解方式,需要添加組件掃描
  • 在配置文件中配置AOP

在實際開發(fā)中,用注解配置AOP比較多,xml配置AOP了解即可

到此這篇關于Spring 使用xml配置AOP的文章就介紹到這了,更多相關spring配置aop內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論