Spring - AOP之AspectJ(注解)

Spring - AOP之AspectJ(注解)_第1张图片Spring - AOP之AspectJ(注解)_第2张图片

Spring - AOP之AspectJ(注解)_第3张图片

Ps:访问修饰符,可有可无。

 

Spring - AOP之AspectJ(注解)_第4张图片Spring - AOP之AspectJ(注解)_第5张图片

Ps:当然 JoinPoint 形参当中也可以去掉的。

Spring - AOP之AspectJ(注解)_第6张图片

Spring - AOP之AspectJ(注解)_第7张图片

Ps:around方法返回值为 Object 是因为针对目标方法如果有返回值的情况。

Spring - AOP之AspectJ(注解)_第8张图片Spring - AOP之AspectJ(注解)_第9张图片Spring - AOP之AspectJ(注解)_第10张图片

package com.imooc.aspectJ.demo1;

public class ProductDao {

    public void save(){
        System.out.println("保存商品...");
    }

    public String update(){
        System.out.println("修改商品...");
        return "hello";
    }

    public void delete(){
        System.out.println("删除商品...");
    }

    public void findOne(){
        System.out.println("查询一个商品...");
        //int i = 1/0;
    }

    public void findAll(){
        System.out.println("查询所有商品...");
//        int j = 1/0;
    }

}
package com.imooc.aspectJ.demo1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 切面类
 */
@Aspect
public class MyAspectAnno {

    @Before(value="myPointcut1()")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知=================="+joinPoint);
    }

    @AfterReturning(value="myPointcut2()",returning = "result")
    public void afterReturing(Object result){
        System.out.println("后置通知=================="+result);
    }

    @Around(value="myPointcut3()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知================");
        Object obj = joinPoint.proceed(); // 执行目标方法
        System.out.println("环绕后通知================");
        return obj;
    }

    @AfterThrowing(value="myPointcut4()",throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("异常抛出通知=============="+e.getMessage());
    }

    @After(value="myPointcut5()")
    public void after(){
        System.out.println("最终通知==================");
    }

    @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))")
    private void myPointcut1(){}

    @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))")
    private void myPointcut2(){}

    @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))")
    private void myPointcut3(){}

    @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))")
    private void myPointcut4(){}

    @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))")
    private void myPointcut5(){}

}



    
    

    
    

    
    
    
package com.imooc.aspectJ.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {

    @Resource(name="productDao")
    private ProductDao productDao;

    @Test
    public void demo1(){
        productDao.save();
        productDao.update();
        productDao.delete();
        productDao.findAll();
        productDao.findOne();
    }

}
运行结果:
前置通知==================execution(void com.imooc.aspectJ.demo1.ProductDao.save())
保存商品...
修改商品...
后置通知==================hello
环绕前通知================
删除商品...
环绕后通知================
查询所有商品...
最终通知==================
查询一个商品...

 

你可能感兴趣的:(Spring,AspectJ,AOP,注解,@Pointcut,#,JavaWeb,#,Spring,#,Spring,教程)