我有以下 spring 伪代码有线 scala 类。
@Service
class Something {
@Value("${some.property}")
val someString : String
// do something with someString in the body
}
但是,这不起作用,因为 someString 是构造函数主体的一部分,并且在构造函数执行之前,spring 无法连接值。
我如何连接@Value 以便它工作,并且不可怕(我当前的解决方案使用包含我需要的所有值的自定义构造函数只是感觉不到“scala”并且看起来很糟糕。
编辑:澄清一下,我目前的解决方案是这样的:
@Service
class Something {
@Autowired
def this(@Value("${some.prop}") prop : String) {
this()
// do other construction stuff here
}
}
我只是觉得它看起来很丑,觉得有更好的方法。当我宁愿在主构造函数上强制执行时,我也不喜欢创建这些辅助构造函数。
编辑2:为了进一步澄清,我希望有一种方法可以做这样的事情:
@Service
class Something(@Value("${some.property}") string : String) {
// use the value in your constructor here
}
因为这看起来要优雅得多。我只是无法让它工作:)