微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用@Autowired of spring with scala

我正在使用scala中的spring,并且在尝试使用trait / superclass注入服务时遇到问题.

这是我的代码

trait MyServiceHolder{
  var myService:MyService = null

  @Autowired
  def setMyService(ms:MyService) = myService = ms
}

@RunWith(classOf[SpringJUnit4ClassRunner])
@ContextConfiguration(Array("file:src/main/webapp/WEB-INF/application-context.xml"))
class MyConcreteClass extends MyServiceHolder{

  def hello() = myService.hello()  

}

这有效:

@RunWith(classOf[SpringJUnit4ClassRunner])
@ContextConfiguration(Array("file:src/main/webapp/WEB-INF/application-context.xml"))
class MyConcreteClass{

  var myService:MyService = null

  @Autowired
  def setMyService(ms:MyService) = myService = ms

  def hello() = myService.hello()  

}

问题是myService在我的测试用例中是null.查看字节码级别(类文件)时,所有注释都存在.有任何想法吗?

解决方法:

在运行测试时,您需要使用Spring TestContext Framework的一种形式让Spring配置您的bean.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐