Spring中AOP下的AspectJ基于注解配置使用

72 阅读1分钟

Spring中AOP下的AspectJ基于注解配置使用

1、导包:spring-aspects-4.3.18.RELEASE.jar(新版IDEA创建Spring项目时会自动下载这个包,没有的就需要下载,确认有aspects这个包)

2、配置:

PS:使用注解,只需配置aop注解生效即可

<?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">
    <!--开启注解-->
    <context:annotation-config/>
    <!--注解的位置-->
    <context:component-scan base-package="com.hwt"></context:component-scan>

    <!--配置aop注解生效-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>

 

3、切面类(增强类):

PS:此种方法不用继承类

package com.hwt.service;

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


@Component("myAspect3")
@Aspect
public class MyAspect3 {
    //声明一个公共切入点
    @Pointcut("execution(* com.hwt.service.*.*(..))")
    public  void myPointcut(){};
    @Before("myPointcut()")
    public void myBefore(){
        System.out.println("myBefore前置通知。。。");
    }
    @After("myPointcut()")
    public void myAfter(){
        System.out.println("myAfter最终通知。。。");
    }
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint joinPoint,Object retValue) throws Throwable {
        System.out.println("myAfterReturning后置通知+返回值。。。"+retValue);
    }
    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("myAround环绕通知。。。");
        System.out.println("myAround开启事务。。。");
        System.out.println("myAround关闭事务。。。");
        proceedingJoinPoint.proceed();
        return proceedingJoinPoint.proceed();
    }
    @AfterThrowing(pointcut = "myPointcut()",throwing = "throwable")
    public void myAfterThrowing(JoinPoint joinPoint,Throwable throwable) throws Throwable {
        System.out.println("myAfterThrowing异常通知+异常。。。"+ throwable.getMessage());
    }
}

4、原业务类:

package com.hwt.service;

import org.springframework.stereotype.Service;

@Service("studentService")
public class StudentService {
    public void delete(){
        System.out.println("删除学生!");
    }
    public void add(){
        System.out.println("添加学生!");
    }
    public void update(){
        System.out.println("修改信息!");
    }
}

5、测试类:

package com.hwt.test;

import com.hwt.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test05 {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans05.xml");

        StudentService studentService = (StudentService) context.getBean("studentService");

        studentService.add();
//        studentService.delete();
//        studentService.update();
    }
}