转载:https://mp.weixin.qq.com/s?__biz=MzI1MTYxNTE0Ng==&mid=2247494047&idx=1&sn=da9d49a4c3d49919c44b93f9310ddc27&chksm=e9f2eb5ade85624c14bb4c5c41907b0adb4849da340169a76634b1f07ca1625b630af2418d82&cur_album_id=1954387956761264133&scene=189&poc_token=HBDRNGWjlVdLiyoKdUy4_cHc0zI654tn6InRJFrK
package com.ywsc.fenfenzhong.aspectj.learn;
public class SayHelloService {
public void say(){
System.out.print("Hello AspectJ");
}
}
package com.ywsc.fenfenzhong.aspectj.learn;
public aspect LogAspect{
pointcut logPointcut():execution(void SayHelloService.say());
after():logPointcut(){
System.out.println("记录日志 ...");
}
}
<aop:aspectj-autoproxy/>
package com.ywsc.fenfenzhong.aspectj.learn;
import org.springframework.stereotype.Component;
@Component
public class SayHelloService {
public void say(){
System.out.print("Hello AspectJ");
}
}
package com.ywsc.fenfenzhong.aspectj.learn;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@After("execution(* com.ywsc.fenfenzhong.aspectj.learn.SayHelloService.*(..))")
public void log(){
System.out.println("记录日志 ...");
}
}
package com.ywsc.fenfenzhong.mongodb;
import com.ywsc.fenfenzhong.aspectj.learn.SayHelloService;
public class TestCase {
public static void main(String[] args) {
SayHelloService sayHelloService = ApplicationUtil.getContext().getBean(SayHelloService.class);
sayHelloService.say();
}
}
Hello AspectJ
记录日志...
<aop:aspectj-autoproxy proxy-target-class="true"/>