How to use Spring Autowired (or manually wired) in Scala object?

后端 未结 8 1637
栀梦
栀梦 2020-12-28 19:12

I am trying to use Spring with Scala. I know Autowired works with Scala class, but I am using a web-framework that requires an object and I want to inject a dao into it. I w

8条回答
  •  [愿得一人]
    2020-12-28 19:51

    I faced same issue.
    I have many services and want to call these @Autowired service from scala objects.
    I tried all the above, None of them worked as my expectation.
    I have an object named JobConfig.scala and I want to autowire TableResolver class and TableResolver class itself autowire many other classes.

    My application is in spring boot and scala.

    1. Add src/main/resources/applicationContext.xml file.
    
    
        
        
    
    
    
    1. Add XmlBeansConfig.scala
    import org.springframework.context.annotation.{Configuration, ImportResource}
    
    @Configuration
    @ImportResource(value = Array("classpath*:applicationContext.xml"))
    class XmlBeansConfig {
    }
    
    1. Inside JobConfig.scala
    object JobConfig{
      def getInstance = this
    
      @Autowired
      var tableResolver: TableResolver = _
    }
    

提交回复
热议问题