Spring AOP笔记

最近研究了下spring的aop,从spring2开始,spring支持基于XML和annotation的两种配置,先来看看基于XML配置的实现。

 

基于XML配置的实现:

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="log" class="com.inter.GetLog"></bean>

    <!--
        注意这里:expression="execution(* com.inter.*.print*(..))"
        括号里面第一个*号代表返回值 接下来  com.inter.*. 是你要切入的代码的大概路径,这里为什么用大概路径来形容呢
        因为这里的意思是符合以com.inter的路径都会作为选择的对象,也不详细介绍,查下就明白了, print*(..)是指
        方法名以print开头的都符合,括号里面的 .. 表示参数是随意的都可以。
    -->
    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="printMethods" expression="execution(* com.inter.*.print*(..))"/>
            <aop:around method="getLog" pointcut-ref="printMethods"/>
        </aop:aspect>
    </aop:config>
   
    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="dealException" expression="execution(* com.inter.*.excep*(..))"/>
            <aop:around method="getLog" pointcut-ref="dealException"/>
        </aop:aspect>
    </aop:config>

 

    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="before" expression="execution(* com.inter.*.bef*(..))"/>
            <aop:before method="before" pointcut-ref="before"/>
        </aop:aspect>
    </aop:config>   


    <!--要织入代码的bean-->
    <bean id="print" class="com.inter.SystemPrint"></bean>
   
</beans>

 

SystemPrin的实现类:

public class SystemPrint implements Print{
   
    public String print(String name){
        String result="hello " + name;
        System.out.println(result);
        return result;
    }

 

    public String before(String name){
        String result="before " + name;
        System.out.println(result);
        return result;
    }   

 

    public void exception(String name) throws Exception{
        System.out.println(name);
        throw new Exception();
    }
}

 

下面是所要织入的代码,也就是我们要用来打印和处理异常:

import org.aspectj.lang.ProceedingJoinPoint;
public class GetLog {   
    public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {       
        System.out.println("===begin===");
        try{
            joinpoint.proceed();
        }
        catch(Exception e)
        {
            System.out.println("log exception!");
        }
        System.out.println("===end=====");
    }

 

   public void before(JoinPoint joint) throws Throwable
    {
        System.out.println("===begin===");
        System.out.println("before!");
    }

 

}

 

编写一个测试类,分别测试上面两个方法,可以得到如下的结果:

===begin===
hello ding
===end=====
-----------------
===begin===
laoding
log exception!
===end=====
-----------------
===begin===
before!
before beforeing

 

下面来看看基于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: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.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <!--基于@AspectJ切面的驱动器,如果没有这句话  切面的代码是不会执行的,可以试下-->
    <aop:aspectj-autoproxy/>


    <bean id="myAspect" class="com.inter.MyAspect"></bean>
    <bean id="hello" class="com.inter.HelloImpl"></bean>
   
</beans>

 

HelloImpl类的实现:

public class HelloImpl implements Hello {   
    public String hello(String name)
    {
        String say = "Hello," + name;

        System.out.println(say);
        return say;
    }
}

 

切面的实现:

//首先这是注释这个类就是切面
@Aspect
public class MyAspect {
    /*这里是注释要切入的方法,AfterReturning是表示方法返回以后进行切入,我这里
       选这个的话是因为日志一般都是在方法执行完成后记录,当然你可以拿Before来试*/
    @Around("execution(* com.inter.*.hello(..))")
    public void doLog(ProceedingJoinPoint joinpoint) throws Throwable{
        String result = (String)joinpoint.proceed();
        System.out.println("---doLog"+result);
    }
}

 

然后编写一个测试类,得到如下测试结果:

-----------------
Hello,xiao
---doLogHello,xiao

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值