【Spring】学习三(AOP&&动态代理/异常通知)

 

目录

 

 

一:AOP

二:Schema-based  实现步骤

三:AspectJ实现方式(异常通知)

 

四:异常通知(Schema-base方式)

五:环绕通知(Schema-base方式)

六:环绕通知(AspectJ方式实现)

七:使用注解(基于AspectJ方式)


 

 

 

一:AOP

 

1、 AOP:中文名称 面向切面编程   (Aspect Oriented Programming)

 

2、正常程序执行流程都是纵向执行流程

                                                             2.1 又叫面向切面编程,在原有纵向执行流程中添加切面

                                                             2.2 不需要修改原有程序代码

                                                                                           高扩展性

                                                                                           原有的功能相当于释放了部分逻辑,让职责更加明确!

 

3、 面向切面编程是什么?

                                       3.1 在原有程序纵向执行的流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程。

 

4、常用概念

                    4.1  原有功能: 切点 

                    4.2 前置通知: 在切点之前执行的功能,before advice。

                    4.3 后置通知:在切点之后执行的功能,after advice。

                          如果切点在执行过程中出现异常,则会出发异常通知,throws advice

                          所有的功能叫做 切面

                    4.4 织入: 把切面嵌入到原有功能的过程叫做织入

 

5、Spring提供两种AOP实现方式

 

                                     5.1 Schema-based

                                                          每个通知都需要实现接口或者类。

                                                          配置Spring配置文件时在 <aop:config>配置

 

                                    5.2 AspectJ

                                                         每个通知不需要实现接口或者类

                                                         配置Spring配置文件是在 <aop:config>的子标签 <aop : aspect>中配置。

 

二:Schema-based  实现步骤

 

1、导入jar包(组件包 aopliance.jar  aspectjweaver.jar

 

2、新建通知类

                      2.1 新建前置通知类(实现 MethodBeforeAdvice

                                           arg0 :   切点方法对象  Method

                                           arg1:    切点方法参数

                                           arg2:    切点在哪个对象中


public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
        System.out.println("执行前置通知");
    }
}

                       

                            2.2 新建后置通知类 (实现 AfterReturningAdvice

                                              arg0:  切点方法返回值

                                              arg1:  切点方法对象

                                              arg2:  切点方法参数

                                              arg3:  切点方法所在类的对象



public class MyAfterAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
        System.out.println("执行后置通知");
    }
}

 

3、配置 Spring 配置文件

                                      3.1 引入aop命名空间

                                      3.2 配置通知类 Bean

                                      3.3  配置切面

                                      3.4  是通配符,匹配任意方法名,任意类名,任意一级别的包名。

                                      3.5   如果想匹配任意一参数,可以在方法中添加 (..)  

 

<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.
xsd">
<!-- 配置通知类对象,在切面中引入 -->
<bean id="mybefore" class="com.bjsxt.advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="com.bjsxt.advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
    <!-- 配置切点 -->
    <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo2())" id="mypoint"/>
    <!-- 通知 -->
    <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
    <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/>
</aop:config>
<!-- 配置 Demo 类,测试使用 -->
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
</beans>

 

4、 编写测试代码


public class Test{
    public static void main(String[] args){    
        /**加载配置文件**/
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取Spring管理的对象
        Demo demo=ac.getBean("demo",Demo.class);
        demo.demo1();
        demo.demo2();
        demo.demo3();

    }
}

5、 运行结果

 

 

三:AspectJ实现方式(异常通知)

                                            (对通知类的要求特别低,但是对配置文件的要求也特别高!!)

 1、 只有当切点报异常才能触发异常通知,

      

2、在Spring中有AspectJ方式提供了异常通知的办法

                                       2.1 如果希望通过 schema-base 实现 需要按照特定的要求自己编写方法.

3、 实现步骤

                     3.1  新建异常通知类,在类中写任意名称的方法


public class MyThrowAdvice{
    public void myexception(Exception e1){
        System.out.println("执行异常通知"+e1.getMessage());
    }
}

                     3.2 在spring 配置文件中配置

                                             <aop:aspect  ref=""> 的ref 属性表示:方法在哪个类中。

                                             <aop:after-throwing method="" throwing="" /> 表示什么后置通知,method表示当触发这个通知时,调用哪个方法!throwing:异常对象名,必须和通知中方法参数名相同(可以不在通知中声明异常对象。)

 



<bean id="mythrow" class="com.bjsxt.advice.MyThrowAdvice"></bean>
<aop:config>
    <aop:aspect ref="mythrow">
        <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
        <aop:after-throwing method="myexception" pointcut-ref="mypoint" throwing="e1"/>
    </aop:aspect>
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>

 

 

四:异常通知(Schema-base方式)

 

1、新建一个异常通知类 并实现 throwsAdvice 接口

                                 1.1  必须自己写方法,且方法名字为 afterThrowing

                                 1.2 有两种参数方式

                                                              必须4个是1个或者

                                 1.3 异常类型要与切点报的异常类型一致

 


public class MyThrow implements ThrowsAdvice{
 public void afterThrowing(Exception ex) throws Throwable {
    System.out.println("执行异常通过-schema-base 方式");
    }
}

 

2、 在ApplicationContext.xml 配置



<bean id="mythrow" class="com.bjsxt.advice.MyThrow"></bean> //方法名一定要是 afterThroeing
<aop:config>
    <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
    <aop:advisor advice-ref="mythrow" pointcut-ref="mypoint" />
    </aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>

 

 

 

五:环绕通知(Schema-base方式)

 

1、大概内容就是把 前置通知和后置通知放在一个通知类中,组成环绕通知

 

2、实现步骤

                   2.1  新建一个类实现 MethodInterceptor



public class MyArround implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
        System.out.println("环绕-前置");
        Object result = arg0.proceed();//放行,调用切点方法
        System.out.println("环绕-后置");
        return result;
    }
}

                  2.2 配置 ApplicatonContext.xml



<bean id="myarround" class="com.bjsxt.advice.MyArround"></bean> //配置通知类
<aop:config>
    //配置切点
    <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>

    <aop:advisor advice-ref="myarround"pointcut-ref="mypoint" />
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>

 

 

 

 

六:环绕通知(AspectJ方式实现)

 

1、新建类不用实现任何东西( 这就是AspectJ方式对通知类的要求很低!

                         1.1 类中方法名任意。

public class MyAdvice {
    public void mybefore(String name1, int age1) {  //前置1 参数为两个
        System.out.println("前置" + name1);
    }

    public void mybefore1(String name1) {              //前置2 参数为一个
        System.out.println("前置:" + name1);
    }

    public void myaftering() {    //afterreturning 只有在切点方法正常执行下才触发
        System.out.println("后置 2");
    }

    public void myafter() {          //任何情况下都会触发!
        System.out.println("后置 1");
    }

    public void mythrow() {             //AspectJ 方式下的异常通知
        System.out.println("异常");
    }
    

    //环绕通知 参数为 ProceedingJoinPoint
    public Object myarround(ProceedingJoinPoint p) throws Throwable {
        System.out.println("执行环绕");
        System.out.println("环绕-前置");
        Object result = p.proceed();
        System.out.println("环绕后置");
        return result;
    }

                           1.2 配置Spring 配置文件

                                          1<aop:after/> 后置通知,是否出现异常都执行!

                                          2<aop:after-returing/> 后置通知,只有当切点正常执行时执行!

                                          3<aop:after/>&&<aop:after-returing/>&&<aop:after-throwing/> 三者的执行顺序跟xml中的配置顺序有关

                                          4excution()括号不能扩上args参数, execution(* com.bjsxt.test.Demo.demo1(String,int)) and args(name1,age1) , 中间使用 and 不能使用 && 由Spring 把 and 解析成 && 。

                                         arg(名称)名称自定义的 顺序demo1(切点方法)(参数,参数) 必须对应,

                                         重点5 <aop:before/> arg-names=" 名称" 来源于 excution=" " 中的 args(),同时arg-names=" 名称"必须和通知方法参数名对应!

                                           

<aop:config>
        <aop:aspect ref="myadvice">

            <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String,int))and args(name1,age1)" id="mypoint"/>  //切点 1

            <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String)) and args(name1)" id="mypoint1"/>      //切点 2

            <aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1"/>

            <aop:before method="mybefore1" pointcut-ref="mypoint1" arg-names="name1"/>
            
<!-- <aop:after method="myafter"
            pointcut-ref="mypoint"/>
            <aop:after-returning method="myaftering"
            pointcut-ref="mypoint"/>
            <aop:after-throwing method="mythrow"
            pointcut-ref="mypoint"/>
            <aop:around method="myarround"
            pointcut-ref="mypoint"/>-->
        </aop:aspect>
    </aop:config>

 

 

 

七:使用注解(基于AspectJ方式)

 

1、 Spring 不会自动去寻找注解,必须告诉Spring哪些包下的类中可能由注解

                     1.1 解决办法:引入  xmlns:context 

<context:component-scan base-package="com.bjsxt.advice"></context:component-scan>

 

2、Component

                     2.1 相当于 <bean/>

                     2.2 如果没有参数的话,把类名首字母变小写,相当于<bean id=" "/>

                     2.3 @Component("自定义名称")

 

3、实现步骤:

                     3.1 在Spring配置文件中设置注解在哪些包下




<context:component-scan base-package="com.bjsxt.advice,com.bjsxt.test"></context:component-scan>



                    3.2 在Demo类中添加 注解 @Componet

                                             在方法上添加@Pointcut("")定义切点


@Component
public class Demo{
    @Pointcut("excution(* com.bjsxt.test.Demo.demo1())")  //表示切点所在地点
    public void demo1() throws Exception{
        System.out.println("demo1");
    }
}

                     3.3 在通知类中配置

                                                @Component 相当于 类被 Spring 管理

                                                @Aspect 相当于 <aop:aspect/>  表示通知方法在当前类中!

@Component   //相当于 <bean>标签,如果没有参数的话,把类名首字母变小写,相当于<bean id=""/>

@Aspect    <相当于 aop:aspect />表示通知方法在当前类中

public class MyAdvice{

    @Before("com.bjsxt.test.Demo.demo1()")    //表示在该包下的方法进行前置通知
    Public void mybefore(){
        System.out.println("前置");
    }
    
    @After("com.bjsxt.test.Demo.demo1()")        //表示在该包下的方法进行后置通知
    public void myafter(){
        System.out.println("后置通知");
    }
    
    @AfterThrowing("com.bjsxt.test.Demo.demo1()")
    public void mythrow(){
        System.out.println("异常通知");
    }
       
    @Around("com.bjsxt.test.Demo.demo1()")
    public Object myarround(ProceedingJoinPoint p) throws Throwable{
        System.out.println("环绕-前置");
        Object result=p.proceed();
        System.out.println("环绕-后置");
        return result;
    }
}

 

                                     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值