AOP 之 @DeclareParents的用法

前言

最近一直在看Spring框架文档,也对@DeclareParents注释有了初步的了解,所以这篇文章仅提供入门参考。

概念

通过注释@DeclareParents,可以提供强大的代理功能。那么如何理解这种代理功能呢?如下图所示,B其实就是代理类,用于增强类A的能力(如添加方法实现)。这里可能会有疑惑:为什么不在A类中直接添加想要的功能呢?

在面向切面编程(AOP)时,为了避免侵入性,减少对业务代码的结构影响,同时也遵循面向切面编程的思想,所以通过代理来增强被代理类的功能是更好的选择。面向对象编程的一个目的是,将非业务代码从业务代码中分离出来,那么应该通过代理实现增强被代理类,而不是将非业务代码(这里指通过代理增强被代理类的功能代码)写进业务代码模块中。

这里写图片描述

案例

通过以下案例,可以更好的理解。

代理类接口
package com.springboot.demo.entity;

public interface ProxyClass {
    void print();
}
代理类接口实现
package com.springboot.demo.entity;

public class ProxyClassImpl implements ProxyClass {
    public void print() {
        System.out.println("this is ProxyClass");
    }
}
被代理类
package com.springboot.demo.entity;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class Person {

    public void out(){
        System.out.println("hello");
    }
}

使用注释@DeclareParents建立代理关系

package com.springboot.demo.aop;

import com.springboot.demo.entity.ProxyClass;
import com.springboot.demo.entity.ProxyClassImpl;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class AopExample {


    @DeclareParents(value = "com.springboot.demo.entity.Person",defaultImpl = ProxyClassImpl.class)
    ProxyClass proxy;


}

上面代码说明,Person类是被ProxyClassImpl类代理,实际上两者互为代理,可以通过测试验证

测试代码
package com.springboot.demo;

import com.springboot.demo.entity.Person;
import com.springboot.demo.entity.ProxyClass;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    Person person;

    @Autowired
    ProxyClass proxyClass2;
    
    @Test
    void contextLoads() {
        ProxyClass proxyClass = (ProxyClass) person;
        proxyClass.print();

        Person person2 = (Person) proxyClass2;
        person2.out();
    }

}

通过注释@Autowired,注入Person类和 ProxyClass接口的实例,然后对两者进行强制转型,然后调用转型后的类的方法,看看是否会报错。

结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9h5Umcja-1583059597194)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200301165029444.png)]

Person类和ProxyClass`接口的实例,然后对两者进行强制转型,然后调用转型后的类的方法,看看是否会报错。

结果

[外链图片转存中…(img-9h5Umcja-1583059597194)]

通过结果,可以看到代理与被代理的关系是相互的,因为你可以通过代理类去得到被代理类的能力,反过来也成立。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
@Around是Spring AOP中的一个注解,用于定义环绕通知。环绕通知是在被通知的方法执行前后都会执行的通知。它可以控制被通知方法的执行,并可以在执行前后进行一些额外的处理逻辑。[1] 在使用@Around注解时,需要编写一个环绕通知方法,方法需要带有ProceedingJoinPoint类型的参数。ProceedingJoinPoint参数可以让我们在通知方法中控制被通知方法的执行,例如在方法执行前后打印日志、计算执行耗时等操作。通过调用ProceedingJoinPoint的proceed()方法,可以继续执行被通知方法,也可以在此中断方法的执行。 例如,下面是一个使用@Around注解的示例: ```java @Aspect @Component public class PerformanceAspect { @Around("execution(* com.example.MyService.myMethod(..))") public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); long executionTime = endTime - startTime; System.out.println("Execution Time: " + executionTime + "ms"); return result; } } ``` 以上示例中,使用@Around注解定义了一个名为measureExecutionTime的环绕通知方法。该方法接收一个ProceedingJoinPoint参数,通过调用proceed()方法执行被通知方法,并计算方法的执行耗时。最后,打印出执行时间。 请注意,上述示例中的切点表达式"execution(* com.example.MyService.myMethod(..))"需要根据实际情况进行修改,以匹配需要添加执行耗时的方法。 通过使用@Around注解,我们可以方便地在Spring AOP中实现对方法的执行耗时的监控和记录。<span class="em">1</span> #### 引用[.reference_title] - *1* [【注解】Spring AOP 面向切面编程之@Around的详细用法](https://blog.csdn.net/weixin_44299027/article/details/124489435)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值