SpringAop-@DeclareParents的作用

@DeclareParents的作用

@DeclareParents是SpringAOP提供的一种功能,允许我们在代理目标类上增加新的行为,即新的方法。

在这里插入图片描述

例子:

先有代理类:ICar接口

package com.liuquanju.aop.dao.car;

/**
 * @Description 汽车接口
 * @Date 下午3:50 2022/5/22
 **/
public interface ICar {

    /**
     * @Author yingjie.liu
     * @Description 轮子
     * @Date 下午3:51 2022/5/22
     **/
    void wheel();

    /**
     * @Author yingjie.liu
     * @Description 车灯
     * @Date 下午3:52 2022/5/22
     **/
    void light();
}

ICar接口实现类:Autotruck

package com.liuquanju.aop.dao.car;

import org.springframework.stereotype.Service;

/**
 * @program: liuquanju-parent
 * @ClassName Autotruck
 * @description:
 * @create: 2022-05-22 15:53
 * @Version 1.0
 **/
@Service
public class Autotruck implements ICar{
    @Override
    public void wheel() {
        System.out.println("this is autotruck wheel ...");
    }

    @Override
    public void light() {
        System.out.println("this is autotruck light ...");
    }
}

ICar接口实现类:Autotruck

package com.liuquanju.aop.dao.car;

import org.springframework.stereotype.Service;

/**
 * @program: liuquanju-parent
 * @ClassName SedanCar
 * @description:
 * @create: 2022-05-22 15:56
 * @Version 1.0
 **/
@Service
public class SedanCar implements ICar {
    @Override
    public void wheel() {
        System.out.println("this is SedanCar wheel ...");
    }

    @Override
    public void light() {
        System.out.println("this is SedanCar light ...");
    }
}

假如我们有如下需求:

  1. ICar接口有n多个实现类,每个类中有n个方法(行为);
  2. 我们需要在ICar的众多实现类中增加一个方法(行为),但又不希望每个实现类都去增加一个新的方法,或者新建类继承原类;

使用@declareParents注解实现

如:

public interface IDriver {
    /**
     * @Author yingjie.liu
     * @Description TODO
     * @Date 下午4:03 2022/5/22
     **/
    void sex();
}

IDriver实现类:

public class WomanDriver implements IDriver{
    @Override
    public void sex() {
        System.out.println("the woman driver...");
    }
}

我们将WomanDriver中的sex()方法引入到上边的SedanCar和Autotruck中

  1. 配置类:
@Configuration
@ComponentScan(value = "com.liuquanju.aop")
// 如果proxyTargetClass设置为true,则为强制使用cglib代理,spring默认使用jdk动态代理
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class MyConfig {
}
  1. 新建切面类
@Component
@Aspect
public class CarAspectj {

    /**
     * @Description @DeclareParents 的结构,由三部分组成:
     * 1. value 属性指定了哪种类型的bean要引入新的接口行为,本例子中是ICar,并在起后接着 "+" ,代表ICar下的所有子类型,非ICar自身。
     * 2. defaultImpl属性指定了提供新的行为的接口的实现类
     * 3. @DeclareParents 标注的类标识需要引入的新提供的行为接口,如例子中IDriver接口,且defaultImpl属性的类(WomanDriver)是IDriver的实现类
     * @Date 下午5:31 2022/5/22
     **/
    @DeclareParents(value = "com.liuquanju.aop.dao.car.ICar+", defaultImpl = WomanDriver.class)
    public IDriver iDriver;


}
  1. 测试类:
public class CarAspectjTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
        ICar sedanCar = (ICar) applicationContext.getBean("sedanCar");
        sedanCar.wheel();

        IDriver womanDriver = (IDriver) sedanCar;
        womanDriver.sex();

    }
}

运行Debug,会发现:sedanCar的代理实现了两个接口,一个是我们实现的ICar接口,一个是IDriver接口,这样便实现了将新的行为引入需要新增新行为的类中
在这里插入图片描述
欢迎各位大佬指正

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值