---
title: "AspectJ使用经验"
author: "hongsoft"
date: "2005-06-20T10:34:00Z"
tags: []
slug: "aspectj"
draft: false
lastmod: "2005-06-20T10:34:00Z"
id: 287461
url: "https://hongsoft.iteye.com/blog/287461"
---

AspectJ使用经验

1）AspectJ1.2版下载后不要解压，直接双击安装  
2）AspectJ5版的语法有了较大变化，请下载AJDT插件进行开发  
3）目前的AJDT开发不支持语法提示，在源代码规模较大的情况下，编译较慢，10万行代码，20个pointcut,  
编译约2分钟  
4)AJDT weave pointcut的方式，是在切点的调用处进行weave,不是在被调方法的前后直接weave,所以在  
JSP等非编译地点的调用是不能拦截的。  
下面是代码段示例：  
/*  
 * Created on 2005-6-14  
 \*/  
package aspectj;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import org.apache.log4j.Logger;  
import org.apache.log4j.PropertyConfigurator;

/\**  
 * @author HongSoft  
 *  
 \*/  
public aspect MonitorAspect {  
    static {  
        //PropertyConfigurator.configure("aspectj\_log.properties");  
    }  
    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    //Log logg = LogFactory.getLog(MonitorAspect.class);  
    //Logger logg=Logger.getLogger(MonitorAspect.class.getName());  
    long beginTime=0;  
    //天气预报  
    pointcut monitorAspect1(): target(QueryWertherCachedTest)&&call(* getWeatherInfobyCity(String));  
    //电话  
    pointcut monitorAspect4():call(\*PhoneService.open(String,int,String));  
    
    private void before(String str)  
    {  
        beginTime=System.currentTimeMillis();  
        System.out.println("before Aspect:" + str);  
        //logg.info("开始调用接口"+str+"的时间是:"+sdf.format(new Date())+"\\n");  
    }  
    private void after(String str)  
    {  
        long duration=System.currentTimeMillis()-beginTime;  
        System.out.println("after Aspect:" + str);  
        //logg.info("结束调用接口"+str+"的时间是:"+sdf.format(new Date())+"\\n该接口调用共耗时"+duration+"毫秒\\n");  
    }

    Object around():monitorAspect1(){  
   before("QueryWertherCached.getWeatherInfobyCity(String))");  
        try  
        {    
              proceed();  
        }catch(Exception e)  
        {                 
              e.printStackTrace();   
        }  
   after("QueryWertherCached.getWeatherInfobyCity(String))");  
   return null;  
    }      
     void around():monitorAspect4(){  
   before("PhoneService.open(String,int,String)");  
        try  
        {    
              proceed();  
        }catch(Exception e)  
        {                 
              e.printStackTrace();   
        }  
   after("PhoneService.open(String,int,String)");           
    }  
}