【问题标题】:@Autowired on a constructor of a Scala class@Autowired 在 Scala 类的构造函数上
【发布时间】:2013-07-31 14:16:11
【问题描述】:

我有一个简单的问题,如何在这个 Scala 类的构造函数上使用 spring @Autowired?

class MessageMBeanExporter(messageDirectory: MessageDirectory) extends MBeanExporter with SmartLifecycle {
      .....
}

【问题讨论】:

    标签: spring scala


    【解决方案1】:

    我没试过,但是根据thisthis link,从2.8+你可以这样做:

    class MessageMBeanExporter @Autowired() (messageDirectory: MessageDirectory) extends MBeanExporter with SmartLifecycle {
          .....
    }
    

    【讨论】:

    • 非常感谢,我会测试它,如果它工作正常,我会接受你的回答
    【解决方案2】:

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

    这可以让你做

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

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

    【讨论】:

    • 这对我不起作用(scala 2.11),但将@Autowired() 放在类名和构造函数之间会......class MyClazz @Autowired() (myBean: Bean)...
    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多