Spring学习笔记(九)--AOP操作演示(AspectJ注解)

AOP操作(AspectJ注解)

1.创建类,在类中定义方法

public class User {
    public void add(){
        System.out.println("add ......");
    }
}

对类中方法进行增强
2.创建增强类(编写增强逻辑)

  • 在增强类中,创建方法,让不同方法代表不同通知类型
//增强的类
public class UserProxy {
    // 前置通知
    public void before(){
        System.out.println("before ......");
    }
}

3.进行通知的配置
(1)在Spring配置文件中,开始注解扫描

<!--开启注解扫描-->
<context:component-scan base-package="com.day9"></context:component-scan>
//注意命名空间也要引入aop和context

(2)使用注解创建User和UserProxy对象

@Component // 被增强的类
public class User {
    public void add(){
        System.out.println("add ......");
    }
}

@Component // 增强类
@Aspect // 生成代理对象
//增强的类
public class UserProxy {
    // 前置通知
    public void before(){
        System.out.println("before ......");
    }
}

(3)在增强的类上面添加注解@Aspect

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

(4)在SPring配置文件中开启生成代理对象

!--开启Aspect生成代理对象-->
<!--如果在类上发现@Aspect注解,就把该类生成对代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.qy"></context:component-scan>

    <!--开启Aspect生成代理对象-->
    <!--如果在类上发现@Aspect注解,就把该类生成对代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

4.配置不同类型的通知
(1)在增强类的里面 ,在作为通知方法上面添加通知类型注解,使用切入点表达式配置

@Component // 增强类
@Aspect // 生成代理对象
public class UserProxy {
    // 前置通知
    //@Before注解表示作为前置通知
    @Before(value = "execution(* com.day9.aopanno.User.add(..))")
    public void before(){
        System.out.println("before ......");
    }
}

5.测试

public class test {
    @Test
    public void test(){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("com/day9/aopanno/bean.xml");
        User user = context.getBean("user", User.class);
        user.add();
    }
}

6.其他类型的通知类型

@Component // 增强类
@Aspect // 生成代理对象
public class UserProxy {
    // 前置通知
    //@Before注解表示作为前置通知
    @Before(value = "execution(* com.day9.aopanno.User.add(..))")
    public void before(){
        System.out.println("before ......");
    }

    //后置通知(也叫做最终通知)
    @After(value = "execution(* com.day9.aopanno.User.add(..))")
    public void after(){
        System.out.println("after ......");
    }

    //返回通知(如果有异常,不会执行返回通知)
    @AfterReturning(value = "execution(* com.day9.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning ......");
    }

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

    //环绕通知
    @Around(value = "execution(* com.day9.aopanno.User.add(..))")
    public void before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前 ......");
        // 执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后 ......");
    }
}
posted @ 2021-08-04 11:08  Emuaer  阅读(53)  评论(0编辑  收藏  举报