Spring源码学习(十七)--基于注解@Aspect配置方式进行Aop测试

一. 被代理的目标对象接口

目标对象接口

package com.xizi.service;

public interface UserService {
    public  void add(String name, int age);
    public  void update(String name, int age);
    public  void delete();
    public  void select(int id,String name);
}

二. 接口的实现类

接口的实现类

package com.xizi.service;

public class UserServiceImpl implements UserService {
    public void add(String name, int age) {
        System.out.println("增加了一个用户111111111");
    }


    public void update(String name, int age) {
        System.out.println("更新了一个用户11111111111111");
        System.out.println("==抛出异常==:" + 1 / 0);
    }

    public void delete() {
        System.out.println("删除了一个用户111111111111----测试该方法被增强,切入点设置在这个类上");

    }

    public void select(int id,String name) {
        System.out.println("查询了一个用户111111111111111");
        System.out.println("id="+id+"  name="+name);
    }
}

三. 使用注解实现AOP(正则表达式)

注解切面

package com.xizi.diy;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;


//使用注解实现AOP
@Aspect
public class AnnotationPointCut {

    /**
     * 例如:execution (* com.xizi.service.UserServiceImpl.*(..))
     * 1、execution(): 表达式主体。
     * 2、第一个*号:表示返回类型,*号表示所有的类型。
     * 3、包名:表示需要拦截的包名,后面的两个点表示当前包和当前包的所有子包,
     * 即com.xizi.service.UserServiceImpl包、子孙包下所有类的方法。
     * 4、第二个*号:表示类名,*号表示所有的类。
     * 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个点表示任何参数。
     **/

    @Before("execution(* com.xizi.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("==========方法执行前=============");
    }
    @After("execution(* com.xizi.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("==========方法执行后=============");
    }


    @AfterThrowing("execution(* com.xizi.service.UserServiceImpl.*(..))")
    public void afterThrowingTest() {
        System.out.println("===========后置异常增强===============");
    }

    @AfterReturning("execution(* com.xizi.service.UserServiceImpl.*(..))")
    public void afterReturningTest() {
        System.out.println("============后置返回增强===============");
    }

    @Around("execution(* com.xizi.service.UserServiceImpl.*(..))")
    public Object aroundTest(ProceedingJoinPoint p) {
        System.out.println("=============环绕增强开始=============");
        Object o = null;
        try {
            o = p.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("=============环绕增强结束==============");
        return o;
    }
}

四. XML配置文件进行注解配置处理

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


    <!--开启注解支持-->
    <aop:aspectj-autoproxy proxy-target-class="true">
        <!-- 指定@Aspect类,支持正则表达式,符合该表达式的切面类才会被应用-->
        <aop:include name="annotationPointCut"></aop:include>
    </aop:aspectj-autoproxy>
    <!--方式三 注解-->
    <!--AspectJ-->
    <bean id="annotationPointCut" class="com.xizi.diy.AnnotationPointCut"/>
    <!--bean-->
    <bean id="userService" class="com.xizi.service.UserServiceImpl"/>
</beans>

测试一个接口方法

    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.select(123,"戏子");
    }

五. 测试结果

**加粗样式**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值