AspectJ在Idea上代码Demo

1,导入所需JAR包

在aspectj1.9下导入JAR包

2,创建pom.xml用maven进行jar管理


    
        org.springframework
        spring-context
        5.0.3.RELEASE
    
    
        org.springframework
        spring-aspects
        5.0.3.RELEASE
    
    
        org.springframework
        spring-test
        5.0.3.RELEASE
        test
    
    
        junit
        junit
        4.12-beta-1
        test
    

3,spring的beans.xml配置




    
    

    
    



4,service

package aop;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {

    public String add(Integer id, String name) {
        String str = "add user id = " + id + " name = " + name;
        System.out.println(str);
        return str;
    }

    public void delete(Integer id) {
        System.out.println("delete user id = " + id);
    }

    public void update(Integer id) throws Exception {
        throw new Exception("更新出错");
    }

}


5,切面页面

package aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class OperateService {

    @Pointcut("execution(* aop.UserService.add(..))")
    public void pointCut(){};

    @Before(value = "execution(* aop.UserService.add(..)) && args(ids,names)", argNames = "ids,names")
    public void beforeAdd(Integer ids, String names) {
        System.out.println("插入前..." + "args.id = " + ids + ";name=" + names);
    }

    @After(value = "execution(* aop.UserService.add(..))&&args(id,name)",argNames = "id,name")
    public void afterAdd(Integer id,String name) {
        System.out.println("插入后  id:"+id+"name:"+name);
    }

    @AfterReturning(pointcut = "pointCut()", returning = "ret")
    public void afterAdd2(JoinPoint joinPoint, Object ret) {
        System.out.println("插入后...2; return = " + ret);
    }

    @AfterThrowing(value = "execution(* aop.UserService.*(..))", throwing = "e")
    public void afterThrowException(Exception e) {
        System.out.println("报错了;e=" + e.getMessage());
    }

/*
    @Around(value = "execution(* aop.UserService.add(..)) && args(id,name)", argNames = "point,id,name")
    public void aroundOperate(ProceedingJoinPoint point, Integer id, String name) {
        System.out.println("around1 id =" + id + " name=" + name);
        try {
            point.proceed();
        } catch (Throwable e) {
        }
        System.out.println("around2");
    }
*/

}

6,main

package aop;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class aopMain {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add(1, "lk");
        try {
            userService.update(1);
        } catch (Exception e) {

        }
    }
}

你可能感兴趣的:(AspectJ在Idea上代码Demo)