Spring面向切面编程AOP连接点和通用切点-----Spring框架

文章详细介绍了SpringAOP中的切面编程,包括各种通知类型(前置、后置、环绕、最终和异常通知),以及如何在SecurityAspect中使用@Before注解实现前置通知以增强LogAspect的行为。
摘要由CSDN通过智能技术生成
package com.powernode.spring6.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
//切面类是需要使用@Aspect注解标注的,标注了就是一个切面
@Aspect
@Order(2)
public class LogAspect //切面
{
    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
    //切面等于通知加上切点
    //通知等于增强,就是说要编写的增强代码
    //这里的Advice以方法的形式出现(因为方法里面写代码)
    //Before注解只要标注,就是一个前置通知,内部写切点表达式
    @Pointcut("execution(* com.powernode.spring6.service..*(..))")
    //定义通用的切点表达式
    public void commonAspect()
    {
        //这个方法只是一个标记,方法名随意,方法体也不需要写东西
    }
    @Before("commonAspect()")
    public void Before(JoinPoint joinPoint)
    {
        //这个joinPoint会在Spring容器中调用这个方法的时候自动传过来
        //我们可以直接使用
        Signature signature = joinPoint.getSignature();
        //获取目标方法的签名,内部有方法的具体信息
        logger.info(signature.getName());
        logger.info("我是一段前置通知");
    }
    //后置通知
    @AfterReturning("commonAspect()")
    public void AfterReturning()
    {
        logger.info("我是一段后置通知");
    }
    //环绕通知
    @Around("commonAspect()")
    public void Around(ProceedingJoinPoint joinPoint) throws Throwable {
        //前面的代码
        logger.info("前环绕通知");
        //执行目标
        joinPoint.proceed();//用来执行目标
        //后面的代码
        logger.info("后环绕通知");
    }
    //最终通知
    //finally语句块中的通知
    @After("commonAspect()")
    public void After()
    {
        logger.info("最终通知");
    }
    //异常通知
    @AfterThrowing("commonAspect()")
    public void AfterThrowing()
    {
        logger.info("异常通知");
    }
}
package com.powernode.spring6.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
//切面类是需要使用@Aspect注解标注的,标注了就是一个切面
@Aspect
@Order(2)
public class LogAspect //切面
{
    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
    //切面等于通知加上切点
    //通知等于增强,就是说要编写的增强代码
    //这里的Advice以方法的形式出现(因为方法里面写代码)
    //Before注解只要标注,就是一个前置通知,内部写切点表达式
    @Pointcut("execution(* com.powernode.spring6.service..*(..))")
    //定义通用的切点表达式
    public void commonAspect()
    {
        //这个方法只是一个标记,方法名随意,方法体也不需要写东西
    }
    @Before("commonAspect()")
    public void Before(JoinPoint joinPoint)
    {
        //这个joinPoint会在Spring容器中调用这个方法的时候自动传过来
        //我们可以直接使用
        Signature signature = joinPoint.getSignature();
        //获取目标方法的签名,内部有方法的具体信息
        logger.info(signature.getName());
        logger.info("我是一段前置通知");
    }
    //后置通知
    @AfterReturning("commonAspect()")
    public void AfterReturning()
    {
        logger.info("我是一段后置通知");
    }
    //环绕通知
    @Around("commonAspect()")
    public void Around(ProceedingJoinPoint joinPoint) throws Throwable {
        //前面的代码
        logger.info("前环绕通知");
        //执行目标
        joinPoint.proceed();//用来执行目标
        //后面的代码
        logger.info("后环绕通知");
    }
    //最终通知
    //finally语句块中的通知
    @After("commonAspect()")
    public void After()
    {
        logger.info("最终通知");
    }
    //异常通知
    @AfterThrowing("commonAspect()")
    public void AfterThrowing()
    {
        logger.info("异常通知");
    }
}
package com.powernode.spring6.service;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(0)//数字越小优先级越高
public class SecurityAspect//安全模块切面
{
    private static final Logger logger = LoggerFactory.getLogger(SecurityAspect.class);
    @Before("com.powernode.spring6.service.LogAspect.commonAspect()")
    public void before()
    {
        logger.info("安全模块前置通知");
    }
}

package com.powernode.spring6.service;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(0)//数字越小优先级越高
public class SecurityAspect//安全模块切面
{
    private static final Logger logger = LoggerFactory.getLogger(SecurityAspect.class);
    @Before("com.powernode.spring6.service.LogAspect.commonAspect()")
    public void before()
    {
        logger.info("安全模块前置通知");
    }
}
package com.powernode.spring6.test;

import com.powernode.spring6.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAOPTest
{
    @Test
    public void TestBefore()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
        orderService.generate();
    }
}
package com.powernode.spring6.test;

import com.powernode.spring6.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAOPTest
{
    @Test
    public void TestBefore()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
        orderService.generate();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值