28 AOP操作术语

1.连接点(哪些方法可以被加强,这些方法被称为连接点)

2.切入点(实际被真正增强的方法)

3.通知(增强):

实际被增强的逻辑部分

多种类型:

 

4.切面

把通知应用dao切入点的过程

AOP操作:

1.Spring 框架一般都是基于 AspectJ 实现 AOP 操作

AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使

用,进行 AOP 操作

2.基于 AspectJ 实现 AOP 操作

1)基于 xml 配置文件实现

2)基于注解方式实现(使用)

3、在项目工程里面引入 AOP 相关依赖

4、切入点表达式

1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强

2)语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )

举例 1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强

execution(* com.atguigu.dao.BookDao.add(..))

举例 2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强

execution(* com.atguigu.dao.BookDao.* (..))

举例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强

execution(* com.atguigu.dao.*.* (..))

演示AOP操作:

1)基于注解

创建两个类User 和 UserProxy

 

package com.atguigu.spring5.AOP;

import org.springframework.stereotype.Component;

@Component
public class User {
    public void add(){
        System.out.println("add..");
    }
}
package com.atguigu.spring5.AOP;

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

@Component
@Aspect //生成增强对象
public class UserProxy {
    @Pointcut(value = "execution(* com.atguigu.spring5.AOP.User.add())")
    public void pointDemo(){

    }

    @Before(value ="pointDemo()")
    public void before(){
        System.out.println("brfore....");
    }

    @After(value ="pointDemo()")
    public void after(){
        System.out.println("After....");
    }
    @AfterReturning(value ="pointDemo()")
    public void afterReturning(){
        System.out.println("AfterReturning....");
    }
    @AfterThrowing(value ="pointDemo()")
    public void afterThrowing(){
        System.out.println("AfterThrowing....");
    }
    @Around(value ="pointDemo()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws  Throwable{
        System.out.println("环绕之前....");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后....");
    }
}

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

    <context:component-scan base-package="com.atguigu.spring5.AOP "></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>



</beans>

test测试文件

package com.atguigu.spring5.AOP;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void test1(){
        ApplicationContext context=new
                ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        user.add();
    }
}

运行结果

增强类排序

排序数字越小

其实他的优先执行级别其实是越来越高的

 

 

 

2)基于配置文件

建两个类

Book

package com.atguigu.spring5.AOPConfig;

public class Book {

    public void buy(){
        System.out.println("buy....");
    }
}

BookPro

package com.atguigu.spring5.AOPConfig;

import org.aspectj.lang.ProceedingJoinPoint;

public class BookPro {
    public void before(){
        System.out.println("before....");
    }
    public void after(){
        System.out.println("after....");
    }
    public void afterreturning(){
        System.out.println("after-returning....");
    }
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("around前....");
        proceedingJoinPoint.proceed();
        System.out.println("around后....");

    }





}

xml配置文件(配置bean)

bean2.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:util="http://www.springframework.org/schema/util"
       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/util https://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!--创建对象-->
        <bean id="book" class="com.atguigu.spring5.AOPConfig.Book"></bean>
        <bean id="bookPro" class="com.atguigu.spring5.AOPConfig.BookPro"></bean>
<!--配置aop增强-->
    <aop:config>
<!--        切入点-->
        <aop:pointcut id="p" expression="execution(* com.atguigu.spring5.AOPConfig.Book.buy())"/>
<!--        配置切面-->
        <aop:aspect ref="bookPro">
<!--            增强作用在具体的方法上-->
            <aop:around method="around" pointcut-ref="p"></aop:around>

            <aop:before method="before" pointcut-ref="p"></aop:before>

            <aop:after method="after" pointcut-ref="p"></aop:after>

            <aop:after-returning method="afterreturning" pointcut-ref="p"></aop:after-returning>



        </aop:aspect>

    </aop:config>


</beans>

测试

 @Test
    public void test2(){
        ApplicationContext context=new
                ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book", Book.class);
        book.buy();
    }

特别注意,这边的程序执行顺序和上面注解形式的不一样

注解的排序不受位置影响

这个配置文件的形式就是根据你xml中 

 

这个配置方法的先后顺序进行输出的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值