10.AOP之xml配置

1.使用IDEA创建工程
在这里插入图片描述
在这里插入图片描述

2.引入项目使用的依赖

<dependencies>
	<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
	</dependency>
</dependencies>

3.编写Spring框架核心配置文件applicationContext.xml
在项目目录“/src/main/resources”下新建applicationContext.xml文件,具体代码如下。

<?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"
       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">
        <!--bean definitions here-->
</beans>

4.编写代码
(1).目标类代码
在项目目录“/src/main/java/com/steven”下新建service目录,并在service目录下新建IAccountService接口和AccountServiceImpl实现类,具体代码如下。

public interface IAccountService {
    void transfer(String outUser,String inUser,Double money);
}
public class AccountServiceImpl implements IAccountService {

    public void transfer(String outUser, String inUser, Double money) {
        System.out.println(outUser + "向" + inUser + "转账" + money + "元");
    }
}

(2).通知类代码
在项目目录“/src/main/java/com/steven”下新建advice目录,并在advice目录下新建MyAdvice类,具体代码如下。

public class MyAdvice {
    public void before() {
        System.out.println("前置通知执行了....");
    }

    public void afterReturning() {
        System.out.println("后置通知执行了....");
    }

    public void afterThrowing() {
        System.out.println("异常通知执行了....");
    }

    public void after() {
        System.out.println("最终通知执行了....");
    }

    /**
     * 环绕通知
     *
     * @param pjp 正在执行的连接点,切点
     * @return
     */
    public Object around(ProceedingJoinPoint pjp) {
        Object proceed = null;
        try {
            System.out.println("前置通知执行了");
            proceed = pjp.proceed();
            System.out.println("后置通知执行了");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常通知执行了");
        } finally {
            System.out.println("最终通知执行了");
        }
        return proceed;
    }
}

5.将自定义的类交给Spring的容器管理并配置切面
(1).applicationContext.xml

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

    <!-- 目标类交给IOC容器 -->
    <bean id="accountService" class="com.steven.service.AccountServiceImpl"></bean>

    <!-- 通知类交给IOC容器 -->
    <bean id="myAdvice" class="com.steven.advice.MyAdvice"></bean>

    <!-- AOP配置 -->
    <aop:config>
        <!-- 配置切面 -->
        <aop:aspect ref="myAdvice">
            <aop:before method="before" pointcut="execution(public void com.steven.service.AccountServiceImpl.transfer(..))"/>
            <!-- 通知是独立存在的 -->
            <!-- <aop:after-returning method="afterReturning" pointcut="execution(public void com.steven.service.AccountServiceImpl.transfer(..))"/> -->
            <!-- <aop:after-throwing method="afterThrowing" pointcut="execution(public void com.steven.service.AccountServiceImpl.transfer(..))"/> -->
            <!-- <aop:after method="after" pointcut="execution(public void com.steven.service.AccountServiceImpl.transfer(..))"/> -->
            <!-- <aop:around method="around" pointcut="execution(public void com.steven.service.AccountServiceImpl.transfer(..))"/> -->
        </aop:aspect>
    </aop:config>
</beans>

(2).切点表达式

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号 * 代替,代表任意
  • 包名与类名之间的一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
  • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

(3).切点表达式抽取
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用pointcut-ref 属性代替pointcut属性来引用抽取后的切点表达式。

<!-- AOP配置 -->
<aop:config>
    <!-- 抽取的切点表达式 -->
    <aop:pointcut id="myPointcut" expression="execution(* com.steven.service.AccountServiceImpl.*(..))"/>

    <!-- 配置切面 -->
    <aop:aspect ref="myAdvice">
        <aop:before method="before" pointcut-ref="myPointcut"/>
        <!-- 通知是独立存在的 -->
        <!-- <aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/> -->
        <!-- <aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/> -->
        <!-- <aop:after method="after" pointcut-ref="myPointcut"/> -->
        <!-- <aop:around method="around" pointcut-ref="myPointcut"/> -->
    </aop:aspect>
</aop:config>

(4).通知类型

<aop:通知类型 method=“通知类中方法名” pointcut=“切点表达式"></aop:通知类型>
名称标签名称说明
前置通知before指定增强的方法在切入点方法之前执行
后置通知afterReturning指定增强的方法在切入点方法之后执行
异常通知afterThrowing指定增强的方法出现异常后执行
最终通知after无论切入点方法执行时是否有异常,都会执行
环绕通知around开发者可以手动控制增强代码在什么时候执行

6.编写测试类
在项目目录“/src/main/java”下新建Test类,具体代码如下。

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        IAccountService accountService = (IAccountService) context.getBean("accountService");
        accountService.transfer("steven", "sherry", 100d);
    }
}
前置通知执行了....
steven向sherry转账100.0

7.工程目录
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值