我想在Grails应用程序中使用服务.但是,它始终为null.我使用的是Grails 1.1版.我怎么解决这个问题?
示例代码:
class A{
String name;
def testService;
static transients=['testService']
}
Run Code Online (Sandbox Code Playgroud)
我可以在域类中使用服务吗?
Bur*_*ith 25
这应该工作.请注意,由于您使用的是"def",因此无需将其添加到瞬态列表中.您是否尝试从静态方法访问它?它是一个实例字段,因此您只能从实例访问它.
将服务注入域类的典型用例是用于验证.自定义验证程序将传递要验证的域类实例,因此您可以从中访问该服务:
static constraints = {
name validator: { value, obj ->
if (obj.testService.someMethod(value)) {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
Rem*_*s B 11
简短的回答是.是的,您可以在域类中使用服务.
下面是一个示例代码,其中域类可以从acegi插件访问authenticate服务.它没有问题.
class Deal {
def authenticateService
def afterInsert() {
def user = authenticateService.userDomain();
....
}
....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11841 次 |
| 最近记录: |