spring---注解实现aop


@Aspect表明这是一个切面类

package com.kuang.diy;

/**
 * @author Administrator
 * @description: TODO
 * @date 2021/11/26 13:46
 */
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
 * 使用注解实现aop
 */
//@Aspect:表明这是一个切面类
@Aspect
public class AnnocationPoint {

    //在切点方法之前执行
    @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("========方法之前执行了===========");
    }

    //在切点方法之后执行
    @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("========方法之后执行了===========");
    }


    /*
        在切点方法前后都可以执行
        在环绕增强是,我们可以给定一个参数,代表我们要获取处理切入的点
     */
    @Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("方法执行前");


        Signature signature = point.getSignature();//获得签名
        System.out.println(signature);

        Object object = point.proceed();

        System.out.println("方法执行后");
    }
}

接口

package com.kuang.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

实现类

package com.kuang.service;
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("实现了增加方法");
    }
    public void delete() {
        System.out.println("实现了删除方法");
    }
    public void update() {
        System.out.println("实现了修改方法");
    }
    public void select() {
        System.out.println("实现了查询方法");
    }
}

applicationContext.xml配置文件

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


    "userService" class="com.kuang.service.UserServiceImpl">
    "log" class="com.kuang.log.Log">
    "afterLog" class="com.kuang.log.AfterLog">




"annocationPoint" class="com.kuang.diy.AnnocationPoint">

    




..