laoding
本来我以为,隐身了别人就找不到我,没有用的,像我这样拉风的男人,无论走到哪里,都像在黑暗中的萤火虫一样,那样的鲜明,那样的出众。我那忧郁的眼神,稀疏的胡茬,那微微隆起的将军肚和亲切的笑容......都深深吸引了众人......
posts - 0,  comments - 37,  trackbacks - 0
上一篇中用XMLSchema配置方式介绍了简单日志实现,这里再用annotation来介绍(注意是spring2.0)

来看接口,这个接口简单的不能再简单了,嘻嘻。
public interface Hello {
    String hello(String name);
}

实现类:

public class SayHello implements Hello {

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

}

切面,里面采用了annotation来注释,也说明了大概意思:

/* 
 * Create Date:2008-11-20 下午03:09:11
 * 
 * Author:dingkm
 * 
 * Version: V1.0
 * 
 * Description:对进行修改的功能进行描述 
 * 
 *  
 
*/
//首先这是注释这个类就是切面
@Aspect
public class MyAspect {
    
//这里是注释要切入的方法,AfterReturning是表示方法返回以后进行切入,我这里
    
//选这个的话是因为日志一般都是在方法执行完成后记录,当然你可以拿Before来试
    @AfterReturning("execution(* *.aspectJ.*.hello(..))")
    
public void doLog(ProceedingJoinPoint joinpoint) throws Throwable{
        String result 
= (String)joinpoint.proceed();
        System.out.println(
"---doLog"+result);
    }
}

下面是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"
>
    
    
<!--基于@AspectJ切面的驱动器,如果没有这句话  切面的代码是不会执行的,可以试下--> 
    
<aop:aspectj-autoproxy/>
    
    
<!--这个bean是作为切面    -->
    
<bean id="myAspect" class="spring2aop.aspectJ.MyAspect"></bean>
    
    
<!--要织入代码的bean-->
    
<bean id="hello" class="spring2aop.aspectJ.SayHello"></bean>

</beans>

<aop:aspectj-autoproxy/>这句很关键哦

再来看测试类:

public class Test {

    
/**  
     *   @Description 方法实现功能描述  
     *   
@param args
     *   void
     *   
@throws  抛出异常说明
     
*/
    
public static void main(String[] args) {
        ApplicationContext act 
= new ClassPathXmlApplicationContext(
        
"applicationContext21.xml");
        Hello h 
= (Hello)act.getBean("hello");
        h.hello(
"laoding");
    }

}

看看结果:

---hello laoding
---hello laoding
---doLog---hello laoding

---hello laoding这个与上一篇文章中提到的道理一样,因为记录日志要取得返回结果,所以执行了两次

最后的那句就是我们要的,这样就达到了记录日志的目的,哈哈,收工回去看PPS中韩魔兽对抗。
posted on 2008-11-25 18:27 老丁 阅读(1709) 评论(0)  编辑  收藏 所属分类: spring

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


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


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿(4)

我参与的团队

文章分类(50)

文章档案(48)

相册

朋友

搜索

  •  

积分与排名

  • 积分 - 95306
  • 排名 - 599

最新评论