如何在 Scala 的注释中对数组使用 Spring 表达式语言

How do I use Spring Expression Language for an Array in an annotation with Scala

我有一个看起来像这样的简单 Scala 项目...

@Configuration
public class CommonConfiguration{
    ...
    @Value("${spring.kafka.topic}")
    public String topic;
    ...
}
@Service
class KafkaService @Autowired()(producer: KafkaTemplate[String, Array[Byte]], config: CommonConfiguration){

  def sendMessage(msg: String): Unit = {
    println(s"Writing the message $msg ${config.topic}")
    producer.send(config.topic, msg.getBytes());
  }

  @KafkaListener(id="test", topics="#{'${spring.kafka.topic}'.split(',')}")
  def consume(record: ConsumerRecord[String, String]): Unit = {
    System.out.println(s"Consumed Strinsg Message : ${record.value()}")
  }

}

这给了我错误...

KafkaService.scala:26: error: type mismatch;
[ERROR]  found   : String("#{\'${spring.kafka.topic}\'.split(\',\')}")
[ERROR]  required: Array[String]
[ERROR]   @KafkaListener(id="test", topics= "#{'${spring.kafka.topic}'.split(',')}")

我尝试根据 this 的建议使用 #{'${spring.kafka.topic}'.split(',')},但我无法让它工作。制片人很好地抓住了话题。如何在 Scala 中使用 Spring 表达式语言?

这是工作 Java 版本...

@Service
public class KafkaJavaService {
    @KafkaListener(id="test", topics="#{'${spring.kafka.topic}'.split(',')}")
    public void consume(ConsumerRecord<String, String> record){
        System.out.println("Consumed String Message : "+record.value())
    }
}

根据 this question,看起来 topics = Array("...") 应该可行。

Reading the @RequestMapping documentation : http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html

It accepts a String array parameter for its path mapping.

So this works using java :

@RequestMapping("MYVIEW")

but in scala I need to use :

@RequestMapping(Array("MYVIEW"))

scala 版本有意义,因为注释需要一个字符串数组。但是为什么上面在 java 中工作,它不应该给出编译时错误吗?

编辑

This is also not technically the same thing because the other way I could use a CSV. Even if the worked it would be a 1 string array unless the Array constructor does some fancyness.

应该没什么区别;正如我所说,如果 String[] 的元素中的表达式解析为 String[],我们将递归地将其分解(展平)。参见 here and here

尝试在这些方法中设置断点;我没有安装 Scala 否则我会看看。