Spring scala对象从类中删除final修饰符

时间:2017-12-23 18:37:05

标签: spring scala final companion-object

我正在SCALA中编写 Spring 应用程序。我不得不改变

class Application {}object Application {}

使生成的main() 静态

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableConfigurationProperties
@Import(Array(classOf[CoreConfiguration]))
object Application extends App {
  override def main(args: Array[String]): Unit = {
    val appCtx = SpringApplication.run(classOf[Application], args: _*)
  }

}

导致SPRING抱怨最终类(最终类应用程序):

BeanDefinitionParsingException: Configuration problem: @Configuration class 'Application' may not be final. Remove the final modifier to continue.

如何在SCALA中创建Spring Application类?

1 个答案:

答案 0 :(得分:1)

在同一文件中创建随播广告对象,即同伴的应用对象。结果类似于:

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableConfigurationProperties
@Import(Array(classOf[CoreConfiguration]))
class Application {
  def run(args: Array[String]): Unit = {
    val appCtx = SpringApplication.run(classOf[Application], args: _*)
  }

}

object Application extends App {
  override def main(args: Array[String]): Unit = {
    new Application().run(args)
  }
}