Spring-AOP

AOP 面向切面编程

1、作用

spring aop使用的是java的动态代理技术

两个都用, Proxy cglib

Spring的AOP编程即是通过动态代理类为原始类的方法添加辅助功能

织入:运行期


2、AOP开发术语

切入点(Pointcut):被Spring切入连接点。(目标方法, 要添加辅助功能方法

通知、增强(Advice):( 辅助功能, 封装辅助功能的方法, 要添加到切入点上的功能)  可以为切入点添加额外功能,分为:前置通知、后置通知、异常通知、环绕通知等。

连接点(Joinpoint)通知加到切入点上的位置, 有前, 后, 环绕...    连接点是程序类中客观存在的方法,可被Spring拦截并切入内容。

目标对象(Target):代理的目标对象(要给哪一个对象加东西,那么这个对象为目标对象)

织入(Weaving): 将通知 添加 到切入点上的动作,我们称之为织入  (编译器 运行期)

代理(Proxy)被AOP织入通知后,产生的结果类。

切面(Aspect):由切点和通知组成,将通知添加到切入点上之后形成的一个整体

引介(Introduction)将一个类中的成员添加到另外一个类中。  或一种特殊的增强,可在运行期为类动态添加Field和Method。


3、环境搭建

导包: 引入AOP相关依赖 aop aopalliance.jar(aop联盟) aspectjweaver.jar(织入包)


          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"

          xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"

          xmlns:p="http://www.springframework.org/schema/p"

          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://mybatis.org/schema/mybatis-spring

http://mybatis.org/schema/mybatis-spring.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

                            ">


配置切面

4、MyAdvice

    1.before

    2.around




5、多个通知执行顺序问题

application.xml


6、切入点表达式

7、引介


8、总结

1.配置切面

       

       

       

       

       

           

           

           

           

           

           

       

   


每个连接点方法都有其特殊的参数

before after :  JoinPoint

around : ProceedingJoinPoint

after-returning :  需要配置文件中配置

after-throwing : 接收异常类型  也许要配置参数名


2.切入点表达式

用来匹配要作用的目标

execution( 返回值类型  全类名.方法名(参数类型))

* : 任意

.. : 任意个

                      expression="execution(java.lang.String com.qianfeng.service.UserServiceImp.fn(java.lang.String))"/>

       

                      expression="execution(* com.qianfeng.service.UserServiceImp.fn(java.lang.String))"/>

       

                      expression="execution(* *.qianfeng.service.UserServiceImp.fn(java.lang.String))"/>

       

                      expression="execution(* *.*.*.*.fn(java.lang.String))"/>

       

                      expression="execution(* *.*.*.*.*(java.lang.String))"/>

       

                      expression="execution(* *.*.*.*.*(..))"/>

       

                      expression="execution(* *(..))"/>

       

                      expression="execution(* *..*.*(..))"/>


3.引介 : 将一个类中的成员拿到另一个类中


配置文件中配置作用的目标

        types-matching : 配置表达式 要匹配的目标

        implement-interface : 接口类型

        default-impl : 接口的实现类

使用 要强转类型

@Test

public void test() {

    ApplicationContext applicationContext =new ClassPathXmlApplicationContext("application4.xml");

    UserServiceImp userServiceImp = applicationContext.getBean(UserServiceImp.class);

    userServiceImp.fn();

    MyInterface myInterface = (MyInterface) userServiceImp;

    myInterface.function();

}

你可能感兴趣的:(Spring-AOP)