码迷,mamicode.com
首页 > 编程语言 > 详细

Spring4(4)——AOP面向切面

时间:2018-10-21 16:01:49      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:ble   ESS   win   软件   重要   signature   命名空间   ase   bean   

1. 面向切面基本概念

面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring 框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

 

2. 实例

  • 1. 前置通知;
  • 2. 后置通知;
  • 3. 环绕通知;
  • 4. 返回通知;
  • 5. 异常通知;

aop切面 命名空间引入:

<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"> 

 

aop切面 配置:

<bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean>

<bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>

<aop:config>
    <aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
        <aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
        <aop:before method="doBefore" pointcut-ref="businessService"/>
        <aop:after method="doAfter" pointcut-ref="businessService"/>
        <aop:around method="doAround" pointcut-ref="businessService"/>
        <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
        <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
    </aop:aspect> 
</aop:config>

 

aop切面 jar 包引入

  • aopalliance.jar
  • aspectjweaver-1.6.6.jar
  • spring-aspects-4.0.6.RELEASE.jar
public class StudentServiceAspect {

  // 前置通知
    public void doBefore(JoinPoint jp){
        System.out.println("类名:"+jp.getTarget().getClass().getName());
        System.out.println("方法名:"+jp.getSignature().getName());
        System.out.println("开始添加学生:"+jp.getArgs()[0]);
    }
    
  //后置通知
    public void doAfter(JoinPoint jp){
        System.out.println("类名:"+jp.getTarget().getClass().getName());
        System.out.println("方法名:"+jp.getSignature().getName());
        System.out.println("学生添加完成:"+jp.getArgs()[0]);
    }
    
  //环绕通知
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("添加学生前");
        Object retVal=pjp.proceed();
        System.out.println(retVal);
        System.out.println("添加学生后");
        return retVal;
    }
    
  //返回通知
    public void doAfterReturning(JoinPoint jp){
        System.out.println("返回通知");
    }
  
//异常通知 public void doAfterThrowing(JoinPoint jp,Throwable ex){ System.out.println("异常通知"); System.out.println("异常信息:"+ex.getMessage()); } }

 

Spring4(4)——AOP面向切面

标签:ble   ESS   win   软件   重要   signature   命名空间   ase   bean   

原文地址:https://www.cnblogs.com/Cocoomg/p/9824859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!