软件编程
位置:首页>> 软件编程>> java编程>> Java中lambda表达式实现aop切面功能

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

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

标签:lambda表达式,aop切面

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com