问题描述
我一直在使用 Laravel 进行编码,现在我想使用 Kotlin 学习 Spring,我的第一个问题是.. 我如何像在 Laravel 中通常做的那样在模型中创建自定义访问器?
在 Laravel 中我们这样做:
public function getTypeAttribute($value) {
return strtoupper($value);
}
如何在 Spring Kotlin 实体模型中执行此操作?谢谢!
解决方法
要在 property
上定义 custom accessors,请使用以下语法
class TestClass {
var name: String = ""
get(){
// You can reference the backing field with identifier `field` in accessors
// print("Current value of field is : $field")
return getNameFromDb()
}
set(value){
setNameInDB(value)
}
}
// Now if you do the following
val name = TestClass().name // This will call the get() block
TestClass().name = "SomeName" // This will call the set(value) block