国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Spring組件開發(fā)模式支持SPEL表達式

2024-07-14 08:43:19
字體:
來源:轉載
供稿:網(wǎng)友

本文是一個 Spring 擴展支持 SPEL 的簡單模式,方便第三方通過 Spring 提供額外功能。

簡化版方式

這種方式可以在任何能獲取ApplicationContext 的地方使用。還可以提取一個方法處理動態(tài) 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;/** * 針對 Spring 實現(xiàn)某些特殊邏輯時,支持 SPEL 表達式 * @author liuzh */public class SpelUtil implements ApplicationContextAware {  /**   * 通過 ApplicationContext 處理時   * @param applicationContext   * @throws BeansException   */  @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);        //根據(jù)自己邏輯處理        //例如獲取 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;/** * 針對 Spring 實現(xiàn)某些特殊邏輯時,支持 SPEL 表達式 * @author liuzh */public class SpelUtil2 implements BeanPostProcessor, BeanFactoryAware, BeanClassLoaderAware {  private BeanFactory beanFactory;  private BeanExpressionResolver resolver;  private BeanExpressionContext expressionContext;  /**   * 解析 SPEL   * @param value   * @return   */  private Object resolveExpression(String value){    String resolvedValue = resolve(value);    if (!(resolvedValue.startsWith("#{") && value.endsWith("}"))) {      return resolvedValue;    }    return this.resolver.evaluate(resolvedValue, this.expressionContext);  }  /**   * 解析 ${}   * @param value   * @return   */  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;  }  /**   * 對 bean 的后置處理   * @param bean   * @param beanName   * @return   * @throws BeansException   */  @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 生命周期調(diào)用順序

擴展 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

靈活運用

利用上述模式可以實現(xiàn)很多便捷的操作。

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

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

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網(wǎng)的支持。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 桃园市| 南平市| 乌审旗| 大理市| 仙居县| 紫云| 东山县| 靖州| 昌乐县| 安新县| 镇远县| 容城县| 麦盖提县| 晋城| 象山县| 伊春市| 新宾| 炉霍县| 双流县| 绥化市| 班戈县| 南溪县| 贺州市| 乌拉特前旗| 台前县| 黑河市| 尚义县| 贵溪市| 金川县| 大冶市| 罗甸县| 盐源县| 江阴市| 泽州县| 双流县| 三河市| 常宁市| 临汾市| 明溪县| 大方县| 遂昌县|