转载自: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. window快捷登陆linux的的设置方式(设置ssh的config配置)

    看看网上其他人如何写的: http://www.xuebuyuan.com/414672.html 文中~的意思是用户目录下的意思: http://blog.csdn.net/newjueqi/art ...

  2. Spring流程

    Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...

  3. Ognl中根元素与非根元素的关系

    Ognl中根元素与非根元素的关系 根元素:可以理解为全局变量 非根元素:局部变量 从两者获取其属性的方式看: Object obj = Ognl.parseExpression(“[1]”); [1] ...

  4. Dark roads(kruskal)

    Dark roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  5. UTF-8 BOM编码格式文件对SSI的影响

    最近在用SSI(Server Side Includes)加载子模块的时候发现一个奇怪的现象,加载完成后的网页老是CSS有问题,被加载模块渲染后老是有空白部分.下面给出简单的示例. 文件a.html的 ...

  6. jquery单页网站导航插件One Page Nav

    这是一个轻量级的jQuery的单页网站导航插件.增加了单击后平滑滚动导航和当你浏览不同的部分时自动选择正确的导航项. changeHash: false, 改变当用户单击导航,就改变changeHas ...

  7. Excel中公式的绝对引用和相对引用单元格

    在Excel的表格中,非常常用的就是公式里的绝对引用和相对引用了,具体情况请看下列表格吧. 步骤1 打开做好的excel表格.公式中的相对单元格引用是基于包含公式和单元格引用的单元格的相对位置,若公式 ...

  8. 关于EJB 时间注解与oracle数据库时间格式

    EJB中Temporal是时间注解,其中TemporalType是时间注解的枚举类型其中包括 TemporalType类型,请看源码/*** Type used to indicate a speci ...

  9. cmd正常启动tomcat,而 从eclipse启动出现 404

    设置Tomcat的路径,启动Tomcat,先测试一下环境,在浏览器中输入http://127.0.0.1:8080/ 提示 404找不到网页.出现这种问题然后试了一下,tomcat在外面直接启动  然 ...

  10. Java格式化输出

    Java的格式化输出等同于String.Format,与C有很大的相似,比如 System.out.printf("%8.2f", x);在printf中,可以使用多个参数,例如: ...