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

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

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

1.前言

在之前的學(xué)習(xí)中,都是使用注解的方式進(jìn)行AOP的配置.其實(shí)使用xml配置文件也可以配置AOP.

2.xml配置AOP

xml配置AOP方法如下:

添加相關(guān)依賴

<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>    

相關(guān)bean放到Spring容器中

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

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

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

在配置文件中開啟組件掃描(因?yàn)槲以趯⑾鄳?yīng)的Bean注入到Spring中時(shí),使用的是注解,如果使用Bean標(biāo)簽,這一步可以省略)

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

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

    <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屬性,值是定義切面時(shí)的id,另一種是使用pointcut屬性,需要指定切點(diǎn)方法的全類名

運(yùn)行結(jié)果:

在這里插入圖片描述

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

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

@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是有兩個(gè)參數(shù)的

以@AfterReturning為例,在切面類中添加對(duì)應(yīng)的普通方法:

@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>

需要注意在設(shè)置AOP時(shí),標(biāo)簽中有returning這樣一個(gè)屬性

運(yùn)行結(jié)果:

在這里插入圖片描述

3. 總結(jié)

xml配置AOP的重要步驟:

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

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

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

相關(guān)文章

最新評(píng)論