首页 > 其他 > 详细

Aop 简单实例

时间:2019-07-01 12:18:40      阅读:72      评论:0      收藏:0      [点我收藏+]

一 , 定义aop

@Aspect
@Component
public class MyAspect {
    //* com 这里有个 空格 !
    @Pointcut("execution(* com.example.demo.Service.HelloServiceImpl.sayHello(..))")
    public void pointCut(){}
 
    @Before("pointCut()")
    public void before()
    {
        System.out.println("befor....");
    }
 
    @AfterReturning("pointCut()")
    public void afterReturning()
    {
        System.out.println("afterReturning....");
    }
 
    @After("pointCut()")
    public void after()
    {
        System.out.println("after....");
    }
 
    @AfterThrowing("pointCut()")
    public void afterThrowing()
    {
        System.out.println("afterThrowing....");
    }
}

 

二 , 定义 service 和 impl

service:

public interface IHelloService {
    void sayHello(String name);
}

impl:

@Service("hello")
public class HelloServiceImpl implements IHelloService {
 
    @Override
    public void sayHello(String name) {
        System.out.println(name +" : hello");
    }
}

 

三 , 测试

@Test
    public void contextLoads() {
        helloService.sayHello("tyler");
    }

 

四 , 结果

技术分享图片

 

Aop 简单实例

原文:https://www.cnblogs.com/hanjun0612/p/11113160.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!