SpringAOP的实现(六)

基于注解的AOP配置(常用)。

  • IUserService接口:
package com.qfedu.aop06;

import java.util.List;

/**
 * Created by HuangCong on 2020/2/26.
 */
public interface IUserService {

    List<Object> getAllUser();

    boolean saveUser(Object user);

    boolean deleteUser(int uid);

    boolean updateUser(Object obj);
}

  • UserServiceImpl实现类:
package com.qfedu.aop06;


import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by HuangCong on 2020/2/26.
 */
@Component("us")    //标注为一个组件
public class UserServiceImpl implements IUserService {
    @Override
    public List<Object> getAllUser() {
        System.out.println("------getAllUser------");
        return null;
    }

    @Override
    public boolean saveUser(Object user) {
        System.out.println("------saveUser------");
        return false;
    }

    @Override
    public boolean deleteUser(int uid) {
        System.out.println("------deleteUser------");
        return false;
    }

    @Override
    public boolean updateUser(Object obj) {
        System.out.println("------updateUser------");
        System.out.println(1/0);//	注意此处是为了引发异常!
        return false;
    }
}

  • MyAspect类:切面类
package com.qfedu.aop06;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 以注解的方式实现的切面类MyAspect
 * 当前类中的五种通知方式均以注解方式完成
 * Created by HuangCong on 2020/2/28.
 */
@Component          //  标注当前类为一个组件
@Aspect             //  标注当前类为一个切面类
public class MyAspect {

    /**
     * @Pointcut 注解为了避免相同的匹配规则被定义多处,专门定义该方法设置执行的匹配规则,各个自行调用即可
     */
    @Pointcut("execution(* com.qfedu.aop06.*.*(..))")
    public void setAll() {

    }

    /**
     * @param jp 连接点
     * @Before 表示该方法为一个前置通知
     */
    @Before("setAll()")
    public void myBefore(JoinPoint jp) {
        /*System.out.println(jp.getArgs());
        System.out.println(jp.toString());
        System.out.println(jp.getTarget());*/
        System.out.println("=============Before==============");
    }

    /**
     * @param jp 连接点
     * @After 表示该方法为一个后置通知
     */
    @After("setAll()")
    public void myAfter(JoinPoint jp) {
        /*System.out.println(jp.getArgs());
        System.out.println(jp.toString());
        System.out.println(jp.getTarget());*/
        System.out.println("=============After==============");
    }

    /**
     * @param pjp 处理连接点
     * @return 返回每个业务方法的返回值
     * @Around 表示该方法为一个环绕通知
     */
    @Around("setAll()")
    public Object myAround(ProceedingJoinPoint pjp) {
        try {
            System.out.println("=============Around-Before==============");
            Object obj = pjp.proceed();
            System.out.println("=============Around-After==============");
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    /**
     * @param jp  连接点
     * @param obj 业务方法的返回值
     * @AfterReturning 表示该方法为一个带有返回值的通知
     */
    @AfterReturning(value = "setAll()", returning = "obj")
    public void myReturn(JoinPoint jp, Object obj) {
        System.out.println("===============Return==================" + obj);
    }

    /**
     * @param jp 连接点
     * @param e  Throwable对象
     * @AfterThrowing 表示该方法为一个带有异常的通知
     */
    @AfterThrowing(value = "setAll()", throwing = "e")
    public void myThrow(JoinPoint jp, Throwable e) {
        System.out.println("=================Throw=================" + e.getMessage());
    }
}

  • beans_aop06.xml:XML文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--context:component-scan:组件扫描
        base-package:指定要扫描的包路径
    -->
    <context:component-scan base-package="com.qfedu.aop06"/>

    <!--aop:aspectj-autoproxy:实现自动代理-->
    <aop:aspectj-autoproxy/>
</beans>
  • TestAOP06
package com.qfedu.aop06;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by HuangCong on 2020/2/28.
 */
public class TestAOP06 {
    @Test
    public void testAOP06(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans_aop06.xml");
        IUserService us = context.getBean("us", IUserService.class);

        Object obj = new Object();
        us.getAllUser();
        us.saveUser(obj);
        us.deleteUser(1);
        us.updateUser(obj);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是聪聪黄吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值