04 注解方式完成aop

前面几节我们使用三种方式完成了代理,接下来,我们将看一下在spring中如何完成aop。

1、前提约束

2、操作步骤

  • 在src/main/resources文件夹下创建一个application-aop-anno.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: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">
<!--    用于扫描约定路径下所有的类,把包含@Component的类进行实例化 -->
    <context:component-scan base-package="net.wanho.aspect"></context:component-scan>
<!--    在上述扫描的基础上,如果有一个@Aspect的类,则会产生代理类 -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
  • 在src/main/java文件夹下创建net.wanho.aspect.MyAspect.java,内容如下:

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

import java.util.Arrays;

@Aspect
@Component
public class MyAspect {

    @Pointcut("execution(* net.wanho.aspect.StudentService.*())")
    public void pointCut(){}

    @After("pointCut()")
    public void testPointcut()
    {
        System.out.println("testPointcut");
    }

    @Before("execution(* net.wanho.aspect.StudentService.*(..))")
    public void security(JoinPoint joinPoint)
    {
        System.out.println(joinPoint.getKind()+"."+joinPoint.getSignature().getName()+":("+Arrays.toString(joinPoint.getArgs())+")");
        System.out.println("权限");
    }

    @Around("execution(* net.wanho.aspect.StudentService.*(int)))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around in");

        Object object = proceedingJoinPoint.proceed();

        System.out.println("around out");
        return object;
    }

    @AfterThrowing("execution(* net.wanho.aspect.StudentService.*(int)))")
    public void afterThrowing()
    {
        System.out.println("exception");
    }

    @AfterReturning("execution(* net.wanho.aspect.StudentService.*(int)))")
    public void afterReturning()
    {
        System.out.println("return");
    }
}
  • 在src/main/java文件夹下创建net.wanho.aspect.StudentService.java
public interface StudentService {
    void query();
}
  • 在src/main/java文件夹下创建net.wanho.aspect.StudentServiceImpl.java
import net.wanho.class134.entity.Student;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component(value = "studentService")
public class StudentServiceImpl implements StudentService {
    @Override
    public void query() {
        System.out.println("查询");
    }
}
  • 在src/main/java文件夹下创建net.wanho.aspect.Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-aop-anno.xml");
        StudentService studentService = applicationContext.getBean("studentService", StudentService.class);

        studentService.query();
    }
}

以上就是采用注解方式完成aop的过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值