使用 @AspectJ 的一个例子

    首先,要求JDK版本为5以上,且需要导入以下9个包:
    1.asm-2.2.3.jar
    2.asm-commons-2.2.3.jar
    3.asm-util-2.2.3.jar
    4.aspectjrt.jar
    5.aspectjweaver.jar
    6.log4j-1.2.14.jar
    7.commons-logging.jar
    8.cglib-nodep-2.1_3.jar
    9.spring.jar
    还是以前面的HelloSpeaker为例子:
package  org.hvp.aspect;

public   class  HelloSpeaker  {
    
public void hello(String name) {
        System.out.print(
"Hi," + name);
    }

}

    使用@AspectJ
package  org.hvp.aspect;

import  org.aspectj.lang.annotation.After;
import  org.aspectj.lang.annotation.Aspect;
import  org.springframework.aop.aspectj.annotation.AspectJProxyFactory;

@Aspect
public   class  HelloSpeakerProxy  {
    @After(
"execution(* hello(..))")
    
public void welcome() {
        System.out.println(
", welcome to our pub.");
    }


    
public HelloSpeaker getHelloSpeakerProxy() {
        AspectJProxyFactory factory 
= new AspectJProxyFactory();
        factory.setTarget(
new HelloSpeaker());
        factory.addAspect(HelloSpeakerProxy.
class);
        
return factory.getProxy();
    }


    
public static void main(String[] args) {
        HelloSpeakerProxy proxy 
= new HelloSpeakerProxy();
        HelloSpeaker helloSpeaker 
= proxy.getHelloSpeakerProxy();
        helloSpeaker.hello(
"Huy Vanpom");
    }

}


你可能感兴趣的:(spring,AOP,jdk,log4j)