繁体   English   中英

Scala Spring Boot自动接线不起作用

[英]Scala spring boot autowiring does not work

我正在尝试将Spring Boot与Scala一起使用。 尽管我可以使用CommandLineRunner来引导它,但是我无法自动连接其他Scala bean。 以下是代码:-

import com.service.UtilService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.{CommandLineRunner, SpringApplication}


@SpringBootApplication
class ScalaMain (@Autowired util:UtilService) extends CommandLineRunner{

  override def run(args: String*): Unit = {
    println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    util.sayHello
  }
}
object ScalaMain {
  def main(args: Array[String]): Unit = {
    SpringApplication.run(classOf[ScalaMain]);
  }
}

这是服务类,将自动连接到上述类中

import org.springframework.stereotype.Service

@Service
class UtilService {

  def sayHello = println("A hello from utilService")
}

当我运行gradle任务“ gradle clean bootRun”时,出现以下错误

APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.bhargo.bootstrap.ScalaMain required a bean of type 'com.bhargo.service.UtilService' that could not be found.


Action:

Consider defining a bean of type 'com.bhargo.service.UtilService' in your configuration.

这是怎么了? 非常感谢您的帮助。

谢谢,

阿玛

我遇到了问题,在使用Java时正在执行此操作,但是在使用scala时有点忘记了。 问题是用于引导的类不在我使用@Service批注创建其bean的其他类的父包中。 我改变了包装的结构,现在它就像一个魅力!

您缺少文件中的软件包声明。 确保你有

package com.bhargo.bootstrap

在服务类文件的顶部

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM