简体   繁体   English

无法从 Scala Spring Boot 应用程序读取 application.properties

[英]Unable to read application.properties from Scala Spring Boot app

First time trying to land a Spring project in Scala but I'm finding some issues when it comes to things that seemed straightforward in Java.第一次尝试在 Scala 中开发 Spring 项目,但是当涉及到在 Java 中看起来很简单的事情时,我发现了一些问题。

This is my current context, an object and a class.这是我当前的上下文,一个对象和一个类。

object Sample {
  def main(args: Array[String]): Unit = { // launch this class via console to execute

    SpringApplication.run(classOf[SampleImpl], args: _*)

    new SampleImpl().doSomething()
  }
}

And then the SampleImpl class which contains all the logic.然后是包含所有逻辑的SampleImpl类。

@SpringBootApplication
class SampleImpl extends Serializable {

  @Value("${myValue}")
  val myValue: String = null

  def doSomething(): Unit = {
    print(myValue) // will always print null, should print "sample"
  }

}

File application.properties does contain the myValue=sample property and the file is detected since if I change the @Value placeholder to something not present in the file, it will complain and won't execute.文件application.properties确实包含myValue=sample属性并且检测到该文件,因为如果我将@Value占位符更改为文件中不存在的内容,它将抱怨并且不会执行。

Sure has to be related with me instantiating the class and maybe creating another parallel context instead of using the true bean?当然必须与我实例化类有关,并且可能创建另一个并行上下文而不是使用真正的 bean?

EDIT编辑

Also tried with the following approach, leaving SampleImpl all the big stuff and Sample as the one running the method and params.还尝试了以下方法,让SampleImpl所有大的东西和Sample作为运行方法和参数的那个。 Still no luck retrieving the value so far.到目前为止,检索价值仍然没有运气。

@SpringBootApplication
@Configuration
class SampleImpl extends Serializable {

  @BeanProperty
  @Value("${myValue}")
  var myValue: String = _

  def doSomething(args: Array[String]): Unit = {
    SpringApplication.run(classOf[SampleImpl], args: _*)
    print(myValue) // will always print null, should print "sample"
  }

}

Try to initialise the value with empty rather than null.尝试使用空而不是 null 来初始化值。 something like就像是

@Value("${myValue}")
val myValue: String = ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scala Spring-Boot应用程序中的IncompatibleClassChangeError - IncompatibleClassChangeError in Scala Spring-Boot application Spring 云数据流自定义 Scala 处理器无法从 Starter Apps 发送/接收数据(SCDF 2.5.1 & Spring Boot 2.2.6) - Spring Cloud Data Flow Custom Scala Processor unable to send/receive data from Starter Apps (SCDF 2.5.1 & Spring Boot 2.2.6) Spring Boot(使用Scala)无法实例化数据源[找不到支持的数据源类型] - Spring boot (with Scala) unable to instantiate DataSource [No supported DataSource type found] 无法使用maven用java和scala编译spring boot项目 - Unable to compile spring boot project with java and scala using maven 我无法从 Scala 中的 properties.conf 加载属性 - I am unable to load the properties from properties.conf in scala 无法从Scala宏读取变量注释 - Unable to read variable annotations from Scala macro lightbend config:在application.properties文件中传递动态值 - lightbend config : pass dynamic values in application.properties file 使用Scala读取Spring Boot ConfigurationProperties - Reading Spring Boot ConfigurationProperties with Scala Scala + Spring boot @Profile注解 - Scala + Spring boot @Profile annotation Scala,Json和Spring Boot Rest - Scala, Json & Spring Boot Rest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM