spring-210728-01---AOP-AspectJ注解

spring-210728-01—AOP-AspectJ注解


创建User类,在类里面定义方法add()
创建增强类,UserProxy类
进行通知的配置
	1. 配置Spring文件,开启注解扫描
	2. 使用注解创建User和UserProxy
	3. 在增强类上面添加注解@Aspect
	4. 配置Spring文件,开启生成代理对象
配置不同的通知类型

演示

User.java

package com.bgy.spring.aoptest;

import org.springframework.stereotype.Component;

// 被增强的类
@Component
public class User {

    public void add(){

        // 测试有异常的情况
//        int a = 10/0;

        System.out.println("user  add.............");
    }
}

UserProxy.java

package com.bgy.spring.aoptest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

// 增强的类
@Component
@Aspect     // 生成代理对象
public class UserProxy {

    // 前置通知
    // 注解@Before 表示作为前置通知
    @Before(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
    public void before(){
        System.out.println("before..........");
    }

    // 最终通知
    // 不管有没有异常,都会执行
    @After(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
    public void after(){
        System.out.println("after.............");
    }

    // 后置通知
    // 如果有异常就不执行了
    @AfterReturning(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning.............");
    }

    // 异常通知
    @AfterThrowing(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing.............");
    }

    // 环绕通知
    @Around(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around   环绕之前.............");

        // 被增强的方法
        proceedingJoinPoint.proceed();

        System.out.println("around   环绕之后.............");
    }
}

bean01.xml

需要引入名称空间:
	xmlns:aop="http://www.springframework.org/schema/aop"
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.bgy.spring.aoptest"></context:component-scan>

    <!--
        开启Aspect生成代理对象
        意思就是只要类上面有注解@Aspect,就生成代理对象。
    -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

TestAop.java

import com.bgy.spring.aoptest.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        User user = context.getBean("user", User.class);

        user.add();
    }
}
// 没有异常的情况,运行结果:
//        around   环绕之前.............
//        before..........
//        user  add.............
//        around   环绕之后.............
//        after.............
//        afterReturning.............

//有异常的运行情况:
//        around   环绕之前.............
//        before..........
//        after.............
//        afterThrowing.............
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值