Scala with Spring:构造函数自动装配

时间:2013-06-26 14:02:13

标签: spring scala constructor autowired

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

   @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]

2 个答案:

答案 0 :(得分:5)

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

这允许你做

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

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

答案 1 :(得分:0)

我们将此样式用于Spring MVC应用程序:

  @Component
  class MyService extends Logging {

    @Autowired
    private val myDao: MyDao = null

  }
相关问题