我正在尝试将 spring boot 与 scala 一起使用。 虽然我已经能够使用 CommandLineRunner 引导它,但我无法 Autowiring 其他 scala beans。以下是代码:-
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]);
}
}
这是服务类,它将 Autowiring 到上述类中
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 注释创建的其他类的父包中。我改变了包结构,现在它就像一个魅力!!!!!!
关于java - Scala Spring Boot Autowiring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42794866/