在scala中使用spring的@Autowired
我正在使用 scala 的 spring,在尝试注入具有特征/超类的服务时遇到问题。
这是我的代码:
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。 当查看字节码级别(类文件)时,所有注释都存在。 有任何想法吗?
i am using spring from scala and i am facing a problem when trying to inject a service with a trait/superclass.
This is my code:
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()
}
This works:
@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()
}
The problem is that myService is null in my testcases. When looking at the bytecode level (class file) all annotations are present. Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 Spring TestContext 的形式框架,在运行测试时由 Spring 配置您的 bean。
You need to use a form of the Spring TestContext Framework to have your beans configured by Spring when running tests.