【问题标题】:Scala with Spring: Constructor autowiringScala 与 Spring:构造函数自动装配
【发布时间】:2013-06-23 17:19:24
【问题描述】:

我正在寻找“惯用”的方式来通过构造函数注入将 Scala 类与 Spring 自动装配。 我尝试过这样的事情:

   @Component
   class MyService @Autowired() ( val myDao: MyDao) extends Logging {
   ...
   }

但是我得到一个错误: bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [MyService]:未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException: MyService.() 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:964) ~[spring-beans-3.0.7.RELEASE.jar:3.0.7.RELEASE]

【问题讨论】:

标签: spring scala constructor autowired


【解决方案1】:

我在 spring-scala 项目中的 Scala 对象的构造函数中添加了对 @Autowired 的支持。这仅(在撰写本文时)在夜间快照中。

这可以让你做

@Component
@Autowired
class Service(dep : Dependency) { }

https://github.com/spring-projects/spring-scala

【讨论】:

  • 酷!期待发布!
  • 急切地等待发布:)。
【解决方案2】:

我们将这种风格用于 Spring MVC 应用程序:

  @Component
  class MyService extends Logging {

    @Autowired
    private val myDao: MyDao = null

  }

【讨论】:

  • 是的,我知道这种方式,但它不像应有的那样 Scala-ish :)
  • 如果你想以 Scala-ish 的方式来做,那就不要使用 spring ;) ...我们一直在尽可能快地放弃这种风格。
猜你喜欢
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2012-05-06
  • 2011-03-22
  • 1970-01-01
相关资源
最近更新 更多