Spring学习第二天

一.AOP

1.AOP:中文名称面向切面编程

2.正常程序的执行顺序都是纵向执行流程

    2.1又叫面向切面编程,在原有的纵向执行程序中加了一个横向切面,类似于下图

  

3.正常程序执行流程都是纵向执行

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

   3.2不需要修改原有代码

        3.2.1高扩展性

        3.2.2原有功能相当于释放了部分逻辑 

4.常用概念

   4.1原有功能:切点,pointcut

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

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

   4.4异常通知:在切点执行过程中触发异常后的通知,throws advice

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

5.spring提供2种aop的实现方式

   5.1Schema-based,每个接口都需要实现接口或者类,配置spring文件时在<aop:config>中配置

   5.2AspectJ,每个接口不需要实现接口或者类,配置spring文件时在<aop:config>中的<aop:aspect>中配置

6.*通配符匹配任意方法名,任意类名,任意一级包名

   如果希望匹配方法中传任意参数则使用(..)

7.配置异常通知的步骤

   7.1只有切点报异常才会触发异常通知

   7.2spring中只有AspectJ提供了异常通知的办法

8.spring中aop配置以及前置后置实现

   

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <bean id="before" class="main.dao.Beforeadvice"></bean>
        <bean id="after" class="main.dao.Afteradvice"/>
        <bean id="demo" class="main.dao.Demo"/>
        <bean id="MyException" class="main.dao.Exceptionadvice"/>
        <aop:config>
               <!--<aop:pointcut id="point" expression="execution(* main.dao.Demo.demo2())"/>-->
               <!--<aop:advisor advice-ref="before" pointcut-ref="point"/>-->
               <!--<aop:advisor advice-ref="after" pointcut-ref="point"/>-->
               <aop:aspect ref="MyException">
                      <aop:pointcut id="point1" expression="execution(* main.dao.Demo.demo2())"/>
                      <aop:after-throwing method="Exception" pointcut-ref="point1" throwing="e"/>
               </aop:aspect>
        </aop:config>


</beans>

 

package main.dao;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Beforeadvice implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("执行前置通知");
    }
}
package main.dao;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class Afteradvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
          System.out.println("后置通知");
    }
}

 

9.AspectJ实现异常通知的通知信息展示

   9.1在配置<aop:after.throwing>时,将throwing空间配置为e

   9.2异常通知的类的参数列表加上(Exception e),

   9.3在异常通知方法中通过e.getMessage()可以输出异常通知的异常信息

package main.dao;

public class Exceptionadvice {
    public void Exception(Exception e){
        System.out.println("异常通知"+e.getMessage());
    }
}

10.环绕通知

    10.1环绕通知就是前置通知+后置通知

    10.2利用列表的参数运行切点方法,这个过程称为"放行",也就是让切点方法执行 

     Object result=methodInvocation.proceed(); 

    10.2配置spring的文件

<bean id="around" class="main.dao.MyAround"/>
        <aop:config>
               <aop:pointcut id="point" expression="execution(* main.dao.Demo.demo2())"/>
               <!--<aop:advisor advice-ref="before" pointcut-ref="point"/>-->
               <!--<aop:advisor advice-ref="after" pointcut-ref="point"/>-->
               <!--<aop:aspect ref="MyException">-->
                      <!--<aop:pointcut id="point1" expression="execution(* main.dao.Demo.demo2())"/>-->
                      <!--<aop:after-throwing method="Exception" pointcut-ref="point1" throwing="e"/>-->
               <!--</aop:aspect>-->
               <aop:advisor advice-ref="around" pointcut-ref="point"/>
        </aop:config>

10.3利用Sechema-based实现环绕通知的接口

package main.dao;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import sun.reflect.MethodAccessor;

import java.lang.reflect.InvocationTargetException;

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

 

 

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值