Spring AOP implementation (6)

AOP's sixth ways: Automatic Proxy

IUserService.java

package com.qfedu.aop06;

import java.util.List;

public interface IUserService {

    /**
     * 获取所有的用户对象列表
     * @return
     */
    List<Object> getAllUser();

    /**
     * 保存用户
     * @param user
     * @return
     */
    boolean saveUser(Object user);

    /**
     * 根据用户uid删除该uid对应的用户信息
     * @param uid
     * @return
     */
    boolean deleteUser(int uid);

    /**
     * 更新指定用户信息
     * @param obj
     * @return
     */
    boolean updateUser(Object obj);


    void getUserByUid();
}

UserServiceImpl.java

package com.qfedu.aop06;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("us")
public class UserServiceImpl implements IUserService {

    @Override
    public List<Object> getAllUser() {
        System.out.println("--------getAllUser----------");
        return new ArrayList<>();
    }

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

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

    @Override
    public boolean updateUser(Object obj) {
        System.out.println("--------updateUser----------");
        return true;
    }

    @Override
    public void getUserByUid() {
        System.out.println("--------getUserByUid----------");
        System.out.println(1 / 0);
        String str = null;
        System.out.println(str.length());
    }
}

beans.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>

MyAspect.java

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
 *
 *      当前类中的五种通知方式均以注解方式完成
 */
@Component          //  标注当前类为一个组件
@Aspect             //  标注当前类为一个切面类
public class MyAspect {

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


    /**
     * @Before 表示该方法为一个前置通知
     * @param jp 连接点
     */
    @Before("setAll()")
    public void myBefore(JoinPoint jp){
        //System.out.println(jp.getArgs());
        System.out.println("this is before.");
    }
    
    /**
     * @After 表示该方法为一个后置通知
     * @param jp 连接点
     */
    @After("setAll()")
    public void myAfter(JoinPoint jp){
        //System.out.println(jp.getArgs());
        System.out.println("this is after.");
    }

    /**
     * @Around 表示该方法为一个环绕通知
     * @param pjp 处理连接点
     * @return 返回每个业务方法的返回值
     */
    @Around("setAll()")
    public Object myAround(ProceedingJoinPoint pjp){
        Object obj = null;
        try {
            System.out.println("this is around before");

            obj = pjp.proceed();


            System.out.println("this is around after");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return obj;
    }

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

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

TestAOP06.java

package com.qfedu.aop06;

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

public class TestAOP06 {

    @Test
    public void testAOP06(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/qfedu/aop06/beans.xml");

        IUserService us = ac.getBean("us", IUserService.class);

        us.getAllUser();
        us.deleteUser(1);
        us.saveUser("zhangsan");
        us.updateUser("lisi");
        us.getUserByUid();
    }
}
Released six original articles · won praise 8 · views 729

Guess you like

Origin blog.csdn.net/zpz2001/article/details/104558217