java - Scala Spring Boot Autowiring 不起作用

标签 java scala spring-boot autowired

我正在尝试将 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/

相关文章:

java - 文件对话框中可接受的扩展名

JSON 数组和 Scala Seq

java - 使用 JPA 将 Spring Boot 连接到 MySQL 时出错

scala - Scala Spark中的NullPointerException,似乎是集合类型引起的吗?

java - 在 vaadin 中提供静态资源

spring - 更改错误凭据错误响应 spring security oauth2

java - 继承的 Spring 3.1 Hibernate 4 异常[不能强制转换为 org.hibernate.mapping.RootClass]

java - 错误 : Projects containing version <unknown> deployment descriptors require XMI-format bindings or extensions files. ibm-web-bnd.xml

java - android 中的 speex 支持

Scala-使用foreach将列表写入文件