6

看起来注解需要 Java 中的常量。我想做:

object ConfigStatics {
  final val componentsToScan = Array("com.example")
}

@PropertySource( ConfigStatics.componentsToScan )   // error: constant value required
class MyConfig extends WebMvcConfigurerAdapter {
}

在哪里

@PropertySource( Array("com.example") ) 
class MyConfig extends WebMvcConfigurerAdapter {
}

很好。

可悲的是,scala 不将静态最终 val 识别为常量值。

这里有什么可做的,还是根本不可能在 scala 中命名常量?

4

2 回答 2

4

componentstoScan不是一个常数,因为我可以改变包含的值:

object ConfigStatics {
  final val componentsToScan = Array("com.example")
  componentsToScan(0) = "com.sksamuel"
}

这将起作用

object ConfigStatics {
  final val componentsToScan = "com.example"
}

@PropertySource(Array(ConfigStatics.componentsToScan))
class MyConfig extends WebMvcConfigurerAdapter {
}
于 2013-09-27T23:50:35.147 回答
4

这看起来像一个错误。

SLS 6.24 说文字数组Array(c1, c2, ...)是一个常量表达式。

SLS 4.1 说常量值定义final val x = e方式x被替换为e.

它不是那样工作的,所以要么是规范错误,要么是实现错误。

  final val j = Array(1,2,3)
  def k = j  // j
  final val x = 3
  def y = x  // 3

这是这个问题的副本,其中retronym 承诺为此开张票。

那是三年前。我想知道他的终端上是否还有一个黄色的便利贴?

于 2013-09-28T04:18:09.767 回答