【问题标题】:@Value to retrieve @Query value@Value 检索@Query 值
【发布时间】:2020-03-25 07:37:12
【问题描述】:

要求

我被要求从存储在 application.properties 文件中的自定义属性中检索查询注释的值。

先前的尝试

我尝试使用@Value 没有成功

@Repository
public interface FooRepository
        extends JpaRepository<Foo, Long> {

  @Value("${db.queries.distance}")
  String distanceQuery; // this raises an error

  @Query(nativeQuery = true, value =distanceQuery)
  ...

Eclipse 标记“distanceQuery”并声明

The blank final field distanceQuery may not have been initialized

并强制我按如下方式初始化变量

    public static final Double distanceQuery = null;

不幸的是,这不是我想要的

问题

是否有解决此问题的解决方法? 提前谢谢!

【问题讨论】:

    标签: spring-boot repository


    【解决方案1】:

    您可以在常量java文件中定义查询,而不是在属性文件中定义,并且可以得到如下值

    @Query(nativeQuery = true, value =Constants.DISTANCE_QUERY)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多