Java中lambda表达式实现aop切面功能

作者:lllllLiangjia 时间:2022-12-02 09:44:59 

背景:最近项目中涉及到自定义线程池中子线程获取父线程的traceId,这个数据的传递过程可以用lamdba表达式进行封装实现的。这让我想到spring容器的 * 缓存。其中的一个缓存singletonFactories就是存放的lambda表达式的。

// 缓存的声明
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
// lambda作为参数调用addSingletonFactory方法
this.addSingletonFactory(beanName, () -> {
   return this.getEarlyBeanReference(beanName, mbd, bean);
});

// addSingletonFactory方法
   protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
       Assert.notNull(singletonFactory, "Singleton factory must not be null");
       synchronized(this.singletonObjects) {
           if (!this.singletonObjects.containsKey(beanName)) {
               // 缓存中添加lambda
               this.singletonFactories.put(beanName, singletonFactory);
               this.earlySingletonObjects.remove(beanName);
               this.registeredSingletons.add(beanName);
           }

}
   }

一些业务逻辑可以通过lambda表达式进行封装,就可以当作一个参数一样进行传递,然后在需要的时候进行执行。但是它的强大并不止于此,还可以当作aop切面进行使用。通过一个demo进行展示

lambda表达式实现切面功能

定义一个函数式接口

@FunctionalInterface
public interface DemoInterface {
   void Demo();
}

创建两个实现类

public class DemoSonOne implements DemoInterface{
   public DemoSonOne(Integer age) {
       this.age = age;
   }

private Integer age;

public Integer getAge() {
       return age;
   }

// 重写接口
   @Override
   public void Demo() {
       System.out.println("I'm DemoSonOne, My age is " + age);
   }
}
public class DemoSonTwo implements DemoInterface{
   public DemoSonTwo(String name) {
       this.name = name;
   }

private String name;

public String getName() {
       return name;
   }
   // 实现接口
   @Override
   public void Demo() {
       System.out.println("I'm DemoSonOne, My name is " + name);
   }
}

客户端

public class DemoMain { // lambda表达式进行封装 public static DemoInterface wrap(final DemoInterface demoInterface){ return () -> { System.out.println("Demo方法要执行了"); demoInterface.Demo(); System.out.println("Demo方法要执行完了"); }; } public static void main(String[] args) { DemoSonOne demoSonOne = new DemoSonOne(18); DemoSonTwo demoSonTwo = new DemoSonTwo("haha"); demoSonOne.Demo(); System.out.println("-----------------------"); demoSonTwo.Demo(); System.out.println("-----------------------"); DemoInterface wrapOne = wrap(demoSonOne); DemoInterface wrapTwo = wrap(demoSonTwo); wrapOne.Demo(); System.out.println("-----------------------"); wrapTwo.Demo(); }}public class DemoMain {

// lambda表达式进行封装
   public static DemoInterface wrap(final DemoInterface demoInterface){
       return () -> {
           System.out.println("Demo方法要执行了");
           demoInterface.Demo();
           System.out.println("Demo方法要执行完了");
       };
   }

public static void main(String[] args) {
       DemoSonOne demoSonOne = new DemoSonOne(18);
       DemoSonTwo demoSonTwo = new DemoSonTwo("haha");
       demoSonOne.Demo();
       System.out.println("-----------------------");
       demoSonTwo.Demo();

System.out.println("-----------------------");
       DemoInterface wrapOne = wrap(demoSonOne);
       DemoInterface wrapTwo = wrap(demoSonTwo);
       wrapOne.Demo();
       System.out.println("-----------------------");
       wrapTwo.Demo();

}
}

执行结果

Java中lambda表达式实现aop切面功能

 执行结果如下,可以看到经过wrap方法封装后的DemoInterface接口对象,执行过程都会走lamdba中的代码。给人一种aop的感觉

缺点

经过wrap方法返回的对象都是DemoInterface类型的,它是接口类型,如果在某种特定的情况下能够确定它是由某个子类类型实力化得到的,想要强转回去,然后获取子类独有的属性,这种情况下会报错。

public static void main(String[] args) {
       DemoSonOne demoSonOne = new DemoSonOne(18);
       // 经过lambda封装,得到接口类型
       DemoInterface wrapOne = wrap(demoSonOne);
       wrapOne.Demo();
       // 由接口类型转换为现实类类型
       DemoSonOne wrapOne1 = (DemoSonOne) wrapOne;
       Integer age = wrapOne1.getAge();
       System.out.println(age);
   }

Java中lambda表达式实现aop切面功能

错误结果显示如下:

Exception in thread "main" java.lang.ClassCastException: class functionInterface.DemoMain$$Lambda$14/0x0000000800066840 cannot be cast to class functionInterface.DemoSonOne (functionInterface.DemoMain$$Lambda$14/0x0000000800066840 and functionInterface.DemoSonOne are in unnamed module of loader 'app')
    at functionInterface.DemoMain.main(DemoMain.java:26)

由此可见该方法进行封装有好处,也有坏处,所以要谨慎使用。 

来源:https://blog.csdn.net/liangjiabao5555/article/details/122966508

标签:lambda表达式,aop切面
0
投稿

猜你喜欢

  • springboot中的springSession的存储和获取实现

    2023-11-04 10:23:14
  • Unity制作小地图和方向导航

    2023-02-07 16:51:02
  • Android应用开发之将SQLite和APK一起打包的方法

    2023-07-03 04:16:07
  • C#如何消除验证码图片的锯齿效果

    2023-11-05 04:49:14
  • BeanUtils.copyProperties使用总结以及注意事项说明

    2023-06-27 18:06:18
  • Spring加载properties文件的两种方式实例详解

    2021-11-20 23:36:28
  • Java任意长度byte数组转换为int数组的方法

    2023-02-15 15:26:32
  • Kotlin基础学习之循环和异常

    2023-05-26 00:42:12
  • Android 自定义Button控件实现按钮点击变色

    2022-12-04 18:18:46
  • 详解Android Activity的启动流程

    2023-07-29 08:06:19
  • java 服务器接口快速开发之servlet详细教程

    2022-11-07 09:37:28
  • Android SQLite数据库增删改查操作的案例分析

    2022-06-06 02:55:03
  • 深入学习Java 热部署的知识

    2023-10-16 18:23:24
  • c# 读取XML文件的示例

    2023-11-04 00:51:17
  • Android实现摇一摇功能

    2023-07-23 20:21:11
  • SpringBoot 创建容器的实现

    2022-04-03 08:41:02
  • Android简单自定义音乐波动特效图

    2022-10-09 15:45:44
  • c#根据文件大小显示文件复制进度条实例

    2022-10-10 10:35:16
  • Android 高版本API方法在低版本系统上的兼容性处理

    2022-08-22 08:51:53
  • Java基础之static关键字的使用讲解

    2023-10-06 01:26:25
  • asp之家 软件编程 m.aspxhome.com