栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring组件开发模式支持SPEL表达式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring组件开发模式支持SPEL表达式

本文是一个 Spring 扩展支持 SPEL 的简单模式,方便第三方通过 Spring 提供额外功能。

简化版方式

这种方式可以在任何能获取ApplicationContext 的地方使用。还可以提取一个方法处理动态 SPEL 表达式。

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.expression.StandardBeanexpressionResolver;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.reflect.Method;

public class SpelUtil implements ApplicationContextAware {
  
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (applicationContext instanceof ConfigurableApplicationContext) {
      ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext;
      ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
      StandardBeanexpressionResolver expressionResolver = new StandardBeanexpressionResolver(beanFactory.getBeanClassLoader());
      for (String definitionName : applicationContext.getBeanDefinitionNames()) {
 BeanDefinition definition = beanFactory.getBeanDefinition(definitionName);
 Scope scope = (definition != null ? beanFactory.getRegisteredScope(definition.getScope()) : null);
 //根据自己逻辑处理
 //例如获取 bean
 Object bean = applicationContext.getBean(definitionName);
 //获取实际类型
 Class targetClass = AopUtils.getTargetClass(bean);
 //获取所有方法
 for (Method method : targetClass.getDeclaredMethods()) {
   //获取自定义的注解(Bean是个例子)
   Bean annotation = AnnotationUtils.findAnnotation(method, Bean.class);
   //假设下面的 value 支持 SPEL
   for (String val : annotation.value()) {
     //解析 ${} 方式的值
     val = beanFactory.resolveEmbeddedValue(val);
     //解析 SPEL 表达式
     Object value = expressionResolver.evaluate(val, new BeanexpressionContext(beanFactory, scope));
     //TODO 其他逻辑
   }
 }
      }
    }
  }
}

上面是完全针对ApplicationContext的,下面是更推荐的一种用法。

推荐方式

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.expression.StandardBeanexpressionResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;

public class SpelUtil2 implements BeanPostProcessor, BeanFactoryAware, BeanClassLoaderAware {
  private BeanFactory beanFactory;
  private BeanexpressionResolver resolver;
  private BeanexpressionContext expressionContext;
  
  private Object resolveexpression(String value){
    String resolvedValue = resolve(value);
    if (!(resolvedValue.startsWith("#{") && value.endsWith("}"))) {
      return resolvedValue;
    }
    return this.resolver.evaluate(resolvedValue, this.expressionContext);
  }
  
  private String resolve(String value){
    if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
      return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
    }
    return value;
  }
  @Override
  public void setBeanClassLoader(ClassLoader classLoader) {
    this.resolver = new StandardBeanexpressionResolver(classLoader);
  }
  @Override
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanFactory = beanFactory;
    if(beanFactory instanceof ConfigurableListableBeanFactory){
      this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanexpressionResolver();
      this.expressionContext = new BeanexpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }
  }
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    return bean;
  }
  
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    //获取实际类型
    Class targetClass = AopUtils.getTargetClass(bean);
    //获取所有方法
    ReflectionUtils.doWithMethods(targetClass, method -> {
      //获取自定义的注解(Bean是个例子)
      Bean annotation = AnnotationUtils.findAnnotation(method, Bean.class);
      //假设下面的 value 支持 SPEL
      for (String val : annotation.value()) {
 //解析表达式
 Object value = resolveexpression(val);
 //TODO 其他逻辑
      }
    }, method -> {
      //TODO 过滤方法
      return true;
    });
    return null;
  }
}

这种方式利用了 Spring 生命周期的几个接口来获取需要用到的对象。

Spring 生命周期调用顺序

扩展 Spring 我们必须了解这个顺序,否则就没法正确的使用各中对象。

完整的初始化方法及其标准顺序是:

  • BeanNameAware 的 setBeanName 方法
  • BeanClassLoaderAware 的 setBeanClassLoader 方法
  • BeanFactoryAware 的 setBeanFactory 方法
  • EnvironmentAware 的 setEnvironment 方法
  • EmbeddedValueResolverAware 的 setEmbeddedValueResolver 方法
  • ResourceLoaderAware 的 setResourceLoader 方法 (仅在应用程序上下文中运行时适用)
  • ApplicationEventPublisherAware 的 setApplicationEventPublisher 方法 (仅在应用程序上下文中运行时适用)
  • MessageSourceAware 的 setMessageSource 方法 (仅在应用程序上下文中运行时适用)
  • ApplicationContextAware 的 setApplicationContext 方法 (仅在应用程序上下文中运行时适用)
  • ServletContextAware 的 setServletContext 方法 (仅在Web应用程序上下文中运行时适用)
  • BeanPostProcessors 的 postProcessBeforeInitialization 方法
  • InitializingBean 的 afterPropertiesSet 方法
  • 自定义初始化方法
  • BeanPostProcessors 的 postProcessAfterInitialization 方法

关闭bean工厂时,以下生命周期方法适用:

  • DestructionAwareBeanPostProcessors 的 postProcessBeforeDestruction 方法
  • DisposableBean 的 destroy 方法
  • 自定义销毁方法

参考:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactory.html

灵活运用

利用上述模式可以实现很多便捷的操作。

Spring 中,使用类似模式的地方有:

  • @Value 注解支持 SPEL(和 ${})
  • @Cache 相关的注解(支持 SPEL)
  • @EventListener 注解
  • @RabbitListener 注解

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号