转载自:http://www.blogjava.net/laoding/articles/242611.html

  一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近几天抽空研究了下AOP,学了些东西,在这里记录下spring2.0的aop配置,以一个简单的记录日志的实例来说明,先介绍下用XMLSchema来配置,下一篇介绍annotation配置,废话不多说,开始吧
先新建个web工程,将spring的包加进去,为方便就把全部的jar包加进去。

先来看个接口,很简单就两个方法

public interface Print {
    public String print(String name);
    public String sleep(String name);
}

接下来是实现类

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

下面是所要织入的代码,也就是我们要用来记录日志的

public class GetLog {
    public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {
        String reslut = (String)joinpoint.proceed();
        //这里是记录日志的
        System.out.println("result==="+reslut);
    }
}

再来看spring配置文件,没有注释的很清楚,可以去网上查查

<?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"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
    
    <!--这个bean是作为切面    -->
    <bean id="log" class="spring2aop.GetLog"></bean>

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

</beans>

测试类:

public class Test {

/**  
     *   @Description 方法实现功能描述  
     *   @param args
     *   void
     *   @throws  抛出异常说明
     */
    public static void main(String[] args) {
        ApplicationContext act = new ClassPathXmlApplicationContext(
        "applicationContext20.xml");
        Print t =(Print)act.getBean("print");
        t.print("ding");
        System.out.println("-----------------");
        t.sleep("laoding");

}

}

运行这个类,得到如下结果:
hello ding
hello ding
result===hello ding
-----------------
laoding is sleep now
laoding is sleep now
result===laoding is sleep now

这里的hello ding 打印了两次,不用担心,这是因为执行到getLog切面类的
 String reslut = (String)joinpoint.proceed();这句代码的时候再执行了一次,这句代码是取回
返回结果的,可以设置个断点来测试下好了这里就输出的result就是记录的日志,当然

这里只是个很简单的实现,但是很简单的实现却很容易说清楚原理。

spring aop简单日志实例的更多相关文章

  1. Spring AOP进行日志记录

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  2. Spring AOP进行日志记录,管理

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  3. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  4. Spring AOP 完成日志记录

    Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046

  5. 使用Spring AOP 实现日志管理(简单教程)

    有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...

  6. spring AOP的注解实例

    上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...

  7. Spring AOP 简单入门笔记 (转)

    分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...

  8. TinyFrame再续篇:整合Spring AOP实现日志拦截

    上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...

  9. spring aop实现日志收集

    概述 使用spring aop 来实现日志的统一收集功能 详细 代码下载:http://www.demodashi.com/demo/10185.html 使用spring aop 来实现日志的统一收 ...

随机推荐

  1. aix lvm_lv_vg

    Aix扩展文件系统,添加新硬盘 Cfgmgr 重新扫描新硬盘 Lspv Chdev –l hdisk3 –a pv=yes Extendvg rootvg hdisk3 note 上面的报错解决 Sm ...

  2. HTML字符实体(Character Entities),转义字符串(Escape Sequence)【转】

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  3. 怎样调通微信支付及微信发货通知接口(Js API)

    怎样调通微信支付及微信发货通知接口(Js API) 微信支付提供了一个支付測试页面,微信支付正式使用须要測通支付.发货通知接口 .告警接口.维权接口.告警接口.维权接口非常easy.支付界面调通也相对 ...

  4. MPAndroidChart绘制图形表

    最近一个项目需要用到表格进行统计显示,本来用的是的achartengine,后来发现一个更加强大的开源框架MPAndroidChart. 下面简单介绍下MPAndroidChart,MPAndroid ...

  5. 【巧妙思维】【4-6】Problem F

    题意:有n个正方体,边长为A[i] 当A[k]-A[p]<=lim 时 k可以放在p上面, 问有多少种放法: 一开始被数据范围吓到了 ,以为是n^3算法,答案是nlogn 从小到大排序,一个一个 ...

  6. datatable赋值行

    datatable复制行:DataTable dt = "";  //这里是填充DataTable数据(""中为一个为datatable类型的值,赋值给dt)D ...

  7. 摩根斯坦利 - 2016年09月8日 面试题 - HashMap

    摩根斯坦利 - 2016年09月8日 面试题: 给定一个 Map<Person, Object> map = new HashMap<Person, Object>(); 放入 ...

  8. HttpServletRequest 各种方法总结(转自百度经验)

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息. req ...

  9. 提取 ECharts 中的svg地图信息

    地图的需求还是蛮大的,全国都要自己画的话,还是需要投入比较大的人力. ECharts中有地图,那我们能不能把里面的地图文件提取出来呢,主要逻辑在map.js中. 看源代码发现,ECharts中地图信息 ...

  10. hdu 5726 GCD 倍增+ 二分

    题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...