如何在spring数据@Query中使用属性 [英] How to use a property in spring data @Query
本文介绍了如何在spring数据@Query中使用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将 application.yml 中的属性注入到 spring 数据 @Query.
I can't manage to inject a property from application.yml to a spring data @Query.
以下导致 EL1008E 错误:
The following results in an EL1008E error:
public interface MyRepository extends JpaRepository<MyEntity, Long> {
@Query("select e from MyEntity e where e.foo = :foo and e.env= ?#{env}")
MyEntity findByFoo(@Param("foo") String foo);
}
根据这个博客可以注入spring的principal的一个属性,和我想做的没有太大区别.
According to this blog it is possible to inject a property of spring's principal, which is not very different from what I would like to do.
对此有任何提示吗?
推荐答案
我真的应该停止提问并在不久之后自己回答问题......这不是故意的.
I should really stop asking questions and answer them by myself shortly after ... That is not on purpose.
提到的博客包含解决方案.添加这个:
The mentioned blog has the solution included. Add this:
public class PropertyEvaluationContextExtension extends EvaluationContextExtensionSupport {
private final MyProps p;
public PropertyEvaluationContextExtension(final MyProps p) {
this.p= p;
}
@Override
public String getExtensionId() {
return "foo";
}
@Override
public MyProps getRootObject() {
return this.p;
}
}
@Configuration
public class PropertyConfig {
private final MyProps p;
public PropertyConfig(final MyProps p) {
this.p= p;
}
@Bean
EvaluationContextExtensionSupport propertyExtension() {
return new PropertyEvaluationContextExtension(p);
}
}
现在 MyProps 的每个属性都可以通过 SpEL 访问.
Now every property of MyProps is accessible via SpEL.
这篇关于如何在spring数据@Query中使用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文