laoding
本来我以为,隐身了别人就找不到我,没有用的,像我这样拉风的男人,无论走到哪里,都像在黑暗中的萤火虫一样,那样的鲜明,那样的出众。我那忧郁的眼神,稀疏的胡茬,那微微隆起的将军肚和亲切的笑容......都深深吸引了众人......
posts - 0,  comments - 37,  trackbacks - 0

一直就用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就是记录的日志,当然
这里只是个很简单的实现,但是很简单的实现却很容易说清楚原理。

posted on 2008-11-25 18:14 老丁 阅读(3435) 评论(4)  编辑  收藏 所属分类: spring

FeedBack:
# re: spring aop简单日志实例(1)
2009-11-07 14:16 | 真烂
垃圾,您还是改行吧。  回复  更多评论
  
# re: spring aop简单日志实例(1)
2009-11-10 16:34 | 老丁
朋友,请自重!如果觉得烂请绕道!
  回复  更多评论
  
# re: spring aop简单日志实例(1)
2009-11-12 11:53 |
ProceedingJoinPoint 少一个类  回复  更多评论
  
# re: spring aop简单日志实例(1)
2011-11-15 15:32 | codeme
<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>
没有这么配置的吧,怎么不写到一起?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
本博客主为学习和复习之用,无关其他,想骂人的绕道
Email:dkm123456@126.com
大家一起交流进步
QQ:283582761


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

留言簿(4)

我参与的团队

文章分类(50)

文章档案(48)

相册

朋友

搜索

  •  

积分与排名

  • 积分 - 95272
  • 排名 - 600

最新评论