springAOP(基于注解开发)

五.springAOP(基于注解开发)
1.导包和springAOP XML开发一样
2.前置通知,后置通知示例:
eg:
applicationContext.xml




    
    
    
    

    
    
        
        
    

切面类MyAspect

// @Aspect注解告诉spring这是一个切面类
@Component("myAspect")
@Aspect
public class MyAspect {

    // 声明公共切点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut(){

    }

    // 单独编写切点
    // @Before("execution(* com.gyf.service.*.*(..))")
    // 引用公共切点
    @Before("myPointcut()")
    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知..." + joinPoint.getSignature().getName());
    }

    // 单独编写切点
    // @AfterReturning(pointcut = "execution(* com.gyf.service.*.*(..))", returning = "retValue")
    // 引用公共切点
    @AfterReturning(pointcut = "myPointcut()", returning = "retValue")
    public void myAfterReturning(JoinPoint joinPoint, Object retValue){
        System.out.println("后置通知..." + "=== 方法名为" + joinPoint.getSignature().getName() + "=== 返回值为" + retValue);
    }
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public boolean add() {
        System.out.println("service 添加用户");
        return userDao.add();
    }
}

UserDaoImpl

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public boolean add() {
        System.out.println("dao 添加用户");
        return true;
    }
}

测试类

@Test
public void test5(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    userService.add();
}
输出结果:
前置通知...add
service 添加用户
dao 添加用户
后置通知...=== 方法名为add=== 返回值为true

3.环绕通知示例:
applicationContext.xml




    
    
    
    

    
    
        
        
    

切面类MyAspect

// @Aspect注解告诉spring这是一个切面类
@Component("myAspect")
@Aspect
public class MyAspect {

    // 声明公共切点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut(){
    
    }

    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知...开启事物");
        Object retObj = proceedingJoinPoint.proceed();
        System.out.println("环绕通知...提交事物");
        return retObj;
    }
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public boolean add() {
        System.out.println("service 添加用户");
        return userDao.add();
    }
}

UserDaoImpl

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public boolean add() {
        System.out.println("dao 添加用户");
        return true;
    }
}

测试类

@Test
public void test5(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    boolean ret = userService.add();
    System.out.println(ret);
}

4.异常通知,最终通知示例:
applicationContext.xml




    
    
    
    

    
    
        
        
    

MyAspect

// @Aspect注解告诉spring这是一个切面类
@Component("myAspect")
@Aspect
public class MyAspect {

    // 声明公共切点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut(){

    }

    @AfterThrowing(pointcut="myPointcut()", throwing="e")
    public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
        System.out.println("异常通知..." + joinPoint.getSignature().getName() + "===" + e.getMessage());
    }

    @After("myPointcut()")
    public void a(JoinPoint joinPoint){
        System.out.println("最终通知..." + joinPoint.getSignature().getName());
    }
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public boolean add() {
        System.out.println("service 添加用户");
        int i = 10/0;
        return userDao.add();
    }
}

UserDaoImpl

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public boolean add() {
        System.out.println("dao 添加用户");
        return true;
    }
}

测试类

@Test
public void test5(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    boolean ret = userService.add();
    System.out.println(ret);
}

输出结果:
service 添加用户
最终通知...add
异常通知...add===/ by zero

你可能感兴趣的:(spring)