毕业设计(四)---spring学习笔记(2)之-AOP

AOP:

AOP中的概念 
Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象(包括切入点的描述和通知的描述)。 

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法, 
因为spring只支持方法型的连接点,实际上joinpoint还可以是field或者构造器。 

Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义。 

Advice(通知):所谓通知是指拦截到jointpoint之后所要做的事情就是通知。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知。 

Target(目标对象):代理的目标对象 

Weave(织入): 指将aspects应用到target对象并导致proxy对象创建的过程称为织入 

Introducton(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field

一张我自己理解的AOP的定义的简单图:



使用AOP的两种方式, 一种是Annotation注解 二种是xml方式。

一:Annotation:

<?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: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-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
        <aop:aspectj-autoproxy/><!-- 启动对@AspectJ注解的支持 -->  
</beans>  
红色是需要添加的。

import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.AfterThrowing;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.Pointcut;  
import org.springframework.stereotype.Component;  
  
@Aspect @Component  
public class MyInterceptor {  
  
/** 
     *@Pointcut :表示规定切入点  
     *execution() 语法规范 
     * 第一个“*”表示任意返回结果类型 
     * “cn.itcast.service.impl.PersonServiceBean”:表示对此类下的所有方法进行拦截, 
     * 如果是cn.itcast.service..*.*:表示对包cn.itcast.service以及子包里所 
有的类的所有方法进行拦截, 
     * (..)表示参数  
     */   
  
      
    @Pointcut("execution(* com.mingbai.springaop.PersonServiceBean.*(..))")  
    private void anyMethod(){}//声明一个切入点   后面的 在前,在后执行的方法,都是根据这一个方法而来。
      
/*  @Before("anyMethod()") 
    public void doAccessCheck(){ 
        System.out.println("前置通知"); 
    }
    //上衣个方法@before 中的内容可以向下面一样写,  
@Before("execution(* com.mingbai.springaop.PersonServiceBean.*(..))") 
    public void doAccessCheck(){ 
        System.out.println("前置通知"); 
    }

*/  
      
    //此时的前置通知,只能拦截到参数个数和类型匹配的方法  
    //args(name)中的name必须和方法doAccessCheck的参数一至  
    @Before("anyMethod() && args(name)")  
    public void doAccessCheck(String name){  
        System.out.println(name+"前置通知");  
    }  
      
/*  @AfterReturning("anyMethod()") 
    public void doAfterReturn(){ 
        System.out.println("后置通知"); 
    }*/  
    //得到方法的返回值  
    @AfterReturning(pointcut="anyMethod()",returning="result")  
    public void doAfterReturn(String result){  
        System.out.println("后置通知  "+result);  
    }  
      
  
    @After("anyMethod()")  
    public void doAfter(){  
        System.out.println("最终通知");  
    }  
      
/*  @AfterThrowing("anyMethod()") 
    public void doAfterThrow(){ 
        System.out.println("异常通知"); 
    }*/  
    @AfterThrowing(pointcut="anyMethod()",throwing="e")  
    public void doAfterThrow(Exception e){  
        System.out.println("异常通知------"+e.getMessage());  
    }  
      
    @Around("anyMethod()")  
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
        System.out.println("环绕通知  开始");  
        Object obj = pjp.proceed();  
        System.out.println("环绕通知  结束");  
        return obj;  
    }  
}  

-------------------------------------------------------------------------------------------------------------------------------

二:xml配置
切面就是一个普通的javabean

import org.aspectj.lang.ProceedingJoinPoint;  
public class MyInterceptor1 {  
  
    public void doAccessCheck(){  
        System.out.println("前置通知-------");  
    }  
      
    public void doAfterReturn(){  
        System.out.println("后置通知");  
    }  
      
  
    public void doAfter(){  
        System.out.println("最终通知");  
    }  
    public void doAfterThrow(){  
        System.out.println("异常通知");  
    }  
      
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
        System.out.println("环绕通知  开始");  
        Object obj = pjp.proceed();  
        System.out.println("环绕通知  结束");  
        return obj;  
    }  
}  


---------------------------------------------------------------------------------------------------------------



<?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:context="http://www.springframework.org/schema/context"   
       xmlns:aop="http://www.springframework.org/schema/aop"  
       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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">     
       
   
   <bean id="per" class="com.mingbai.springaop.PersonServiceBean"/>  
     <bean id="myInterceptor" class="com.mingbai.springaop.MyInterceptor1"/>  
     <!--    
     <aop:config>  
        <aop:aspect id="asp" ref="myInterceptor">  
            <aop:pointcut id="mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref="mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>
     -->   
     <!-- 只是拦截返回类型为java.lang.String的方法     
     <aop:config>  
        <aop:aspect id="asp" ref="myInterceptor">   <!-- 定义切面 -->
            <aop:pointcut id="mycut" expression="execution(java.lang.String com.mingbai.springaop.*.*(..))"/>  <!-- 定义切入点 -->
            <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  <!-- 切入点前置通知 -->
            <aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref="mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   -->   
   <!-- 返回非void的方法 -->  
   <aop:config>  
        <aop:aspect id="asp" ref="myInterceptor">  
            <aop:pointcut id="mycut" expression="execution(!void com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref="mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref="mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref="mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref="mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  

     
</beans>  













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值