11.AOP之注解配置

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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.steven"/>

    <!-- AOP的自动代理 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

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

public interface IAccountService {
    void transfer(String outUser,String inUser,Double money);
}
@Service
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类,具体代码如下。

@Aspect
@Component
public class MyAdvice {
    @Before("execution(public void com.steven.service.AccountServiceImpl.transfer(..))")
    public void before() {
        System.out.println("前置通知执行了....");
    }

//    @AfterReturning("execution(public void com.steven.service.AccountServiceImpl.transfer(..))")
//    public void afterReturning() {
//        System.out.println("后置通知执行了....");
//    }
//
//    @AfterThrowing("execution(public void com.steven.service.AccountServiceImpl.transfer(..))")
//    public void afterThrowing() {
//        System.out.println("异常通知执行了....");
//    }
//
//    @After("execution(public void com.steven.service.AccountServiceImpl.transfer(..))")
//    public void after() {
//        System.out.println("最终通知执行了....");
//    }
//
//    /**
//     * 环绕通知
//     *
//     * @param pjp 正在执行的连接点,切点
//     * @return
//     */
//    @Around("execution(public void com.steven.service.AccountServiceImpl.transfer(..))")
//    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;
//    }
}

(3).切点表达式

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

(4).切点表达式抽取

@Aspect
@Component
public class MyAdvice {
    @Pointcut("execution(* com.steven..*.*(..))")
    public void myPoint() {
    }

    @Before("MyAdvice.myPoint()")
    public void before() {
        System.out.println("前置通知...");
    }
}

(5).通知类型

名称注解名称说明
前置通知@Before指定增强的方法在切入点方法之前执行
后置通知@AfterReturning指定增强的方法在切入点方法之后执行
异常通知@AfterThrowing指定增强的方法出现异常后执行
最终通知@After无论切入点方法执行时是否有异常,都会执行
环绕通知@Around开发者可以手动控制增强代码在什么时候执行

当前四个通知组合在一起时,执行顺序如下,
@Before -> @After -> @AfterReturning(@AfterThrowing)

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.工程目录
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值