【问题标题】:How to setup SpEL contextProvider for @Query如何为@Query 设置 SpEL contextProvider
【发布时间】:2021-04-12 07:12:50
【问题描述】:

如何设置上下文提供程序以使我的 SpEL 扩展在 @Query("...") 中可用?

我们使用 r2dbc 和 postgres。

据我了解,我需要使用ReactiveExtensionAwareQueryMethodEvaluationContextProvider 注册我的扩展程序,但我找不到任何有关如何执行此操作的文档。是否有特定的 bean 或 @Configuration 可供使用?

我找到了this unit test。它是 monogo 而不是 r2dbc,但我认为这是相同的原理 - 但是因为它是一个单元测试,它没有显示如何在我的正常代码中设置上下文。

尝试像这样实现它不会在 SpEL 中提供my() 方法

@Configuration
class MySpELExtensionConf {

  @Bean
  fun mySpELExtension() : ReactiveEvaluationContextExtension {
    return ReactiveSpelExtension()
  }

  class ReactiveSpelExtension : ReactiveEvaluationContextExtension {

    override fun getExtension(): Mono<out EvaluationContextExtension> {
      return Mono.just(SpelExtension.INSTANCE)
    }

    override fun getExtensionId(): String {
      ReactiveQueryMethodEvaluationContextProvider.DEFAULT
      return "mySpELExtension"
    }
  }

  enum class SpelExtension : EvaluationContextExtension {
    INSTANCE;

    override fun getRootObject(): Any? {
      return this
    }

    override fun getExtensionId(): String {
      return "mySpELExtension"
    }

    fun my(): String {
      return "x"
    }
  }
}

我现在在应用程序上下文中看到了mySpELExtension,但是在@Query 中使用my() 方法是不可能的:

interface MyRepository : ReactiveCrudRepository<MyEntity, Long> {
  @Query("""
    ...
    :#{my()}
    ...
  """)
  fun findByMyQuery() : Flux<MyEntity>
}

结果

EL1004E: Method call: Method my() cannot be found on type java.lang.Object[]

【问题讨论】:

    标签: spring spring-boot spring-el spring-data-r2dbc


    【解决方案1】:

    我想出了一个使用PostBeanProcessor 的解决方案。

    @Configuration
    class MySpELExtensionConf {
    
      companion object {
        // list of provided extensions
        val contextProviderWithExtensions =
          ReactiveExtensionAwareQueryMethodEvaluationContextProvider(listOf(ReactiveSpelExtension()))
      }
    
      /**
       * Registers the customizer to the context to make spring aware of the bean post processor.
       */
      @Bean
      fun spELContextInRepositoriesCustomizer(): AddExtensionsToRepositoryBeanPostProcessor {
        return AddExtensionsToRepositoryBeanPostProcessor()
      }
    
      /**
       * Sets the [contextProviderWithExtensions] for SpEL in the [R2dbcRepositoryFactoryBean]s which makes the extensions
       * usable in `@Query(...)` methods.
       */
      class AddExtensionsToRepositoryBeanPostProcessor : BeanPostProcessor {
        override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any {
          if (bean is R2dbcRepositoryFactoryBean<*, *, *>) {
            bean.addRepositoryFactoryCustomizer { it.setEvaluationContextProvider(contextProviderWithExtensions) }
          }
          return bean
        }
      }
    
     // ... extension implementation as in question ...
    }
    

    请注意,自定义 R2dbcRepositoryFactoryBean 适用于 CoroutineCrudRepository,您可能需要根据您使用的存储库自定义不同的工厂 bean。

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 1970-01-01
      • 2021-09-06
      • 2016-05-30
      • 2014-09-03
      • 2020-01-03
      • 1970-01-01
      • 2015-03-12
      • 2019-05-11
      相关资源
      最近更新 更多