SpringAop注解版

SpringAop注解版

项目总览

aop01.png

先在pom.xml中添加依赖:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.13</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.13</version>
        </dependency>
</dependencies>

AspectJ的注解及其描述

daUEe.png

MyAspect02切面类的内容:

package com.ssm.aspectj.annotation;

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

@Aspect
@Component
public class MyAspect02 {
    //定义切入点表达式
    @Pointcut("execution(* com.ssm.aspectj.*.*(..))")
    //使用一个返回值是void、方法体为空的方法来作为切入点
    public void myPointCut(){}
    //前置通知
    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint){
        System.out.print("before notify: simulate power examine...");
        System.out.print("object class: " + joinPoint.getTarget());
        System.out.println(",is waved method is: " + joinPoint.getSignature().getName());
    }
    //后置通知
    @AfterReturning("myPointCut()")
    public void myAfterReturning(JoinPoint joinPoint){
        System.out.print("endAfterReturning notify: simulate write log...");
        System.out.println(",is waved method is: " + joinPoint.getSignature().getName());
    }
    //环绕通知
    /*
    * ProceedingJoinPoint是JoinPoint的子接口,表示可执行目标方法
    * 1.必须是Object返回值
    * 2.必须接受一个参数,类型为ProceedingJoinPoint
    * 3.必须throws Throwing
    * */
    @Around("myPointCut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("around start: start target method before, simulate start transaction");
        Object object = proceedingJoinPoint.proceed();
        System.out.println("around end: start target method after, simulate end");
        return object;
    }
    //异常通知
    @AfterThrowing(value = "myPointCut()",throwing = "e")
    public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
        System.out.println("error notify: error" + e.getMessage());
    }
    //最终通知
    @After("myPointCut()")
    public void myAfter(){
        System.out.println("end notify: simulate method end after realise resources");
    }
}

applicationContext.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="com.ssm"/>
    <!--启动基于注解的声明式AspectJ支持-->
    <aop:aspectj-autoproxy/>
</beans>

Main内容:

package com.ssm.aspectj.annotation;

import com.ssm.aspectj.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnotation {
    public static void main(String[] args) {
        String xmlPath = "com.ssm.aspectj.annotation/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.addUser();
    }
}

posted @ 2022-05-22 22:33  wei_good  阅读(58)  评论(0编辑  收藏  举报