激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring組件開(kāi)發(fā)模式支持SPEL表達(dá)式

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

2021-06-24 10:31isea533 Java教程

今天小編就為大家分享一篇關(guān)于Spring組件開(kāi)發(fā)模式支持SPEL表達(dá)式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

本文是一個(gè) spring 擴(kuò)展支持 spel 的簡(jiǎn)單模式,方便第三方通過(guò) spring 提供額外功能。

簡(jiǎn)化版方式

這種方式可以在任何能獲取applicationcontext 的地方使用。還可以提取一個(gè)方法處理動(dòng)態(tài) spel 表達(dá)式。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
/**
 * 針對(duì) spring 實(shí)現(xiàn)某些特殊邏輯時(shí),支持 spel 表達(dá)式
 * @author liuzh
 */
public class spelutil implements applicationcontextaware {
  /**
   * 通過(guò) applicationcontext 處理時(shí)
   * @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);
        //獲取實(shí)際類型
        class<?> targetclass = aoputils.gettargetclass(bean);
        //獲取所有方法
        for (method method : targetclass.getdeclaredmethods()) {
          //獲取自定義的注解(bean是個(gè)例子)
          bean annotation = annotationutils.findannotation(method, bean.class);
          //假設(shè)下面的 value 支持 spel
          for (string val : annotation.value()) {
            //解析 ${} 方式的值
            val = beanfactory.resolveembeddedvalue(val);
            //解析 spel 表達(dá)式
            object value = expressionresolver.evaluate(val, new beanexpressioncontext(beanfactory, scope));
            //todo 其他邏輯
          }
        }
      }
    }
  }
}

上面是完全針對(duì)applicationcontext的,下面是更推薦的一種用法。

推薦方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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;
/**
 * 針對(duì) spring 實(shí)現(xiàn)某些特殊邏輯時(shí),支持 spel 表達(dá)式
 * @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;
  }
  /**
   * 對(duì) bean 的后置處理
   * @param bean
   * @param beanname
   * @return
   * @throws beansexception
   */
  @override
  public object postprocessafterinitialization(object bean, string beanname) throws beansexception {
    //獲取實(shí)際類型
    class<?> targetclass = aoputils.gettargetclass(bean);
    //獲取所有方法
    reflectionutils.dowithmethods(targetclass, method -> {
      //獲取自定義的注解(bean是個(gè)例子)
      bean annotation = annotationutils.findannotation(method, bean.class);
      //假設(shè)下面的 value 支持 spel
      for (string val : annotation.value()) {
        //解析表達(dá)式
        object value = resolveexpression(val);
        //todo 其他邏輯
      }
    }, method -> {
      //todo 過(guò)濾方法
      return true;
    });
    return null;
  }
}

這種方式利用了 spring 生命周期的幾個(gè)接口來(lái)獲取需要用到的對(duì)象。

spring 生命周期調(diào)用順序

擴(kuò)展 spring 我們必須了解這個(gè)順序,否則就沒(méi)法正確的使用各中對(duì)象。

完整的初始化方法及其標(biāo)準(zhǔn)順序是:

  • beannameaware 的 setbeanname 方法
  • beanclassloaderaware 的 setbeanclassloader 方法
  • beanfactoryaware 的 setbeanfactory 方法
  • environmentaware 的 setenvironment 方法
  • embeddedvalueresolveraware 的 setembeddedvalueresolver 方法
  • resourceloaderaware 的 setresourceloader 方法 (僅在應(yīng)用程序上下文中運(yùn)行時(shí)適用)
  • applicationeventpublisheraware 的 setapplicationeventpublisher 方法 (僅在應(yīng)用程序上下文中運(yùn)行時(shí)適用)
  • messagesourceaware 的 setmessagesource 方法 (僅在應(yīng)用程序上下文中運(yùn)行時(shí)適用)
  • applicationcontextaware 的 setapplicationcontext 方法 (僅在應(yīng)用程序上下文中運(yùn)行時(shí)適用)
  • servletcontextaware 的 setservletcontext 方法 (僅在web應(yīng)用程序上下文中運(yùn)行時(shí)適用)
  • beanpostprocessors 的 postprocessbeforeinitialization 方法
  • initializingbean 的 afterpropertiesset 方法
  • 自定義初始化方法
  • beanpostprocessors 的 postprocessafterinitialization 方法

關(guān)閉bean工廠時(shí),以下生命周期方法適用:

  • destructionawarebeanpostprocessors 的 postprocessbeforedestruction 方法
  • disposablebean 的 destroy 方法
  • 自定義銷毀方法

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

靈活運(yùn)用

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

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

  • @value 注解支持 spel(和 ${})
  • @cache 相關(guān)的注解(支持 spel)
  • @eventlistener 注解
  • @rabbitlistener 注解

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

原文鏈接:https://blog.csdn.net/isea533/article/details/84100428

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕一二三区芒果 | 久久久久久久一区二区三区 | 色综合777| 国产精品午夜一区 | 欧美一级毛片一级毛片 | 日韩av成人 | 久久蜜桃精品一区二区三区综合网 | 久久人人爽人人爽人人片av高清 | 精品国产一区二区三区四区阿崩 | 蜜桃视频日韩 | 日日狠狠久久偷偷四色综合免费 | 欧美精品免费一区二区三区 | 国产一区国产二区在线观看 | 国产精品一二三区在线观看 | 久久精品中文字幕一区二区 | 福利在线免费 | 国产精品成人久久 | 斗罗破苍穹在线观看免费完整观看 | 午夜视频免费播放 | 国产精品久久久久久久久久iiiii | 亚洲精品a在线观看 | 在线视频 欧美日韩 | 特级毛片免费视频 | 色婷婷综合久久久久中文 | 久久久久久久久久久久网站 | 三级国产三级在线 | 日产精品久久久一区二区福利 | 国产精品亚洲一区二区三区久久 | 欧美一区久久久 | 一级黄色在线免费观看 | 毛片视频免费观看 | 在线播放91 | 视频一区二区在线播放 | 国产成人羞羞视频在线 | 一区二区三区视频在线播放 | 色欧美视频 | 性大片性大片免费 | 精品国产一区二区三区在线 | 特片网久久| 午夜视频久久久 | 永久免费黄色片 |