AOP 面向切面编程

公用的方法就是切面,织入到切点 

横跨多个功能模块

 把切面织入到连接点的位置

目标对象中有很对位置都可以被织入(这些可以被织入的位置被称为连接点joinpoint)

切点pointcunt是连接点中真正织入的点(也就是说不是所有的连接点都织入代码,有些连接点虽然可以被织入,但是实际上并不织入)

 

定义一个切面类(在这个类上面打上@Aspect注解)

首先@Before表示这个方法在目标方法执行前被调用

execution(* com.example.MyService.*(..))表示去MyService这个类里面寻找切点

@Aspect
@Component
public class MyAspect {

    @Before("execution(* com.example.MyService.*(..))")
    public void function2() {
        // 在目标方法执行之前执行的逻辑
        System.out.println("Before advice executed");
    }

}

 真正的切点就是MyService这个类中加了@MyAnnotation注解的方法,所以最后的效果就是在执行function1()方法之前会先运行function2()方法

@Service
public class MyService {

    @MyAnnotation
    public void function1() {
        // 目标方法逻辑
    }

}

 

@Component
@Aspect
public class AlphaAspect
{
   //定义两个内容,切点pointcut,通知advice

  @Point("execution()")//这里规定将切面织入到哪些位置,即哪些包下的哪些方法
  public void  pointcut()
  {
   

  }

   @Before("pointcut()")
   public   void   before()
   {
       System.out.println("before");
   }

   @After("pointcut()")
   public   void   after()
   {
       System.out.println("after");
   }

}

通知advice分成:

前置通知:  before advice

后置通知: after  advice

环绕通知: around advice

返回值后通知:afterReturning advice

异常通知:afterThrowing advice

实战:利用before,在调用方法之前记录谁在什么时候调用了这个方法,这个日志只会在后台控制台输出,不会在前端浏览器打印

按以下格式打印:

用户XXXX在xxxxx时刻访问了 com.nowcoder.community.service.xxx

@Component
@Aspect
public ServiceLogAspect
{
  private  static  final Logger  logger=LoggerFactory.getLogger(ServiceLogAspect.class);


  //定义两个内容,切点pointcut,通知advice


  @Point("execution(com.nowcoder.community.service.*.*(..))")//这里规定将切面织入到哪些位置,即哪些包下的哪些方法
  public void  pointcut()
  {
  

  }

   @Before("pointcut()")
   public   void   before()
   {
      //获取当前用户ip
    


      //获取当前时间
      String  now=new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());


      logger.info(.....);

    }
}
   

在业务方法的前后增加一些通用的操作(最常见的通用操作就是打印日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的AOP面向切面编程的测试代码示例,使用Spring框架实现: 首先,创建一个切面类 `LoggingAspect`,用于定义切面逻辑: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 然后,创建一个测试服务类 `UserService`,用于演示AOP的应用: ```java import org.springframework.stereotype.Service; @Service public class UserService { public void createUser(String username) { System.out.println("Creating user: " + username); } public void deleteUser(String username) { System.out.println("Deleting user: " + username); } } ``` 最后,创建一个Spring Boot应用程序,并在启动类中进行配置: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); UserService userService = context.getBean(UserService.class); userService.createUser("John"); userService.deleteUser("John"); } } ``` 在上述示例中,`LoggingAspect` 切面类使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。 当运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。 这是一个简单的AOP面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值