Spring之基于注解的AOP配置

238 阅读2分钟

一、添加依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

二、AOP相关注解

@Aspect 注解声明该类为切面类。

@Before 申明该方法为前置通知,属性value指定切入点表达式,或指定切入点表达式的引用

@AfterReturning申明该方法为后置通知,属性value指定切入点表达式,或指定切入点表达式的引用

@AfterThrowing 申明该方法为异常通知,属性value指定切入点表达式,或指定切入点表达式的引用

@After 申明该访问为最终通知,属性value指定切入点表达式,或指定切入点表达式的引用

@Around 申明该方法是环绕通知,属性value指定切入点表达式,或指定切入点表达式的引用
@Pointcut 指定切入点表达式,属性value:指定表达式的内容

三、创建通知类

@Component("webLog")
//@Aspect 申明该类是一个切面类
@Aspect
public class WebLog {
    
    @Pointcut("execution(* cn.ybzy.service.impl.*.*(..))")
    private void webLog(){
    
    }
  
    /**
     * 环绕通知
     * @param proceedingJoinPoint Spring提供的一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法相当于明确调用切入点方法。
     *
     *            该接口可以作为环绕通知的方法参数,spring中的环绕通知提供的是一种可以在代码中手动控制增强方法的方式。
     * @return
     */
 /*   @Around(value = "webLog()")
   // @Around("execution(* cn.ybzy.service.impl.*.*(..))")
    public Object aroundLog(ProceedingJoinPoint proceedingJoinPoint){
        Object returnValue = null;
        try{
            //获取方法执行所需的参数
            Object[] args = proceedingJoinPoint.getArgs();
    
            System.out.println("前置通知。。。");
            
            //调用切入点方法
            returnValue = proceedingJoinPoint.proceed(args);
            
            System.out.println("后置通知。。。");
            
            
            return returnValue;
            
        }catch (Throwable t){
            System.out.println("异常通知。。。");
            throw new RuntimeException(t);
        }finally {
            System.out.println("最终通知。。。");
        }
    }
   */
      /**
     * 前置通知
     */
    @Before(value = "execution(* cn.ybzy.service.impl.*.*(..))")
    public  void beforeLog(){
        System.out.println("前置通知。。。");
    }
    
    /**
     * 后置通知
     */
    @AfterReturning(value = "execution(* cn.ybzy.service.impl.*.*(..))")
    public  void afterReturningLog(){
        System.out.println("后置通知。。。");
    }
    /**
     * 异常通知
     */
    @AfterThrowing(value = "webLog()")
    public  void afterThrowingLog(){
        System.out.println("异常通知。。。");
    }
    
    /**
     * 最终通知
     */
    @After(value = "webLog()")
    public  void afterLog(){
        System.out.println("最终通知。。。");
    }
    
   
}

四、创建接口及实现

public interface IUserService {
   void saveUser();
 }


@Service("userService")
public class UserServiceImpl implements IUserService {
    
    @Override
    public void saveUser() {
        System.out.println("保存...");
        //int i=1/0;
    }
}

五、使用配置类代替XML配置文件

@Configuration
@ComponentScan(basePackages="cn.ybzy")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
使用spring.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="cn.ybzy"></context:component-scan>

    <!-- 配置spring开启注解AOP的支持 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

六、执行测试

public class WebLogTest {

    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        IUserService as = (IUserService)ac.getBean("userService");
        as.saveUser();
    }
}

1.使用前置、后置、异常、最终通知
在这里插入图片描述
2.使用环绕通知
在这里插入图片描述