lambda表达式实现aop切面

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

lambda表达式实现aop切面

lllllLiangjia   2022-05-23 我要评论

背景:最近项目中涉及到自定义线程池中子线程获取父线程的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();
 
    }
}

执行结果

 执行结果如下,可以看到经过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);
    }

错误结果显示如下:

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)

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们