Grails 域控制器中的依赖注入
我正在尝试创建自定义约束。我已将逻辑放入服务中:
class RegExpManagerService {
boolean transactional = false
def messageSource
def lookupRegexp(regExpression,Locale locale) {
def pattern = messageSource.getMessage( regExpression,null,locale )
return pattern
}
def testRegexp(regExpression,text,Locale locale) {
return text ==~ lookupRegexp(regExpression,locale)
}
}
并尝试将其注入我的域控制器中:
class Tag extends IbidemBaseDomain {
def regExpManagerService
static hasMany=[itemTags:ItemTag]
static mapping = {
itemTags fetch:"join"
}
//Long id
Date dateCreated
Date lastUpdated
String tag
// Relation
Tagtype tagtype
// Relation
Customer customer
// Relation
Person updatedByPerson
// Relation
Person createdByPerson
static constraints = {
dateCreated(nullable: true)
lastUpdated(nullable: true)
tag(blank: false,validator: {val,obj ->
regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
})
tagtype(nullable: true)
customer(nullable: true)
updatedByPerson(nullable: true)
createdByPerson(nullable: true)
}
String toString() {
return "${tag}"
}
}
当执行约束时,我收到此错误:
2009-08-24 18:50:53,562 [http-8080-1] ERROR errors.GrailsExceptionResolver - groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
I'm trying to create a a custom constraint. I've put the logic in a service:
class RegExpManagerService {
boolean transactional = false
def messageSource
def lookupRegexp(regExpression,Locale locale) {
def pattern = messageSource.getMessage( regExpression,null,locale )
return pattern
}
def testRegexp(regExpression,text,Locale locale) {
return text ==~ lookupRegexp(regExpression,locale)
}
}
and tried to inject it in my domain controller:
class Tag extends IbidemBaseDomain {
def regExpManagerService
static hasMany=[itemTags:ItemTag]
static mapping = {
itemTags fetch:"join"
}
//Long id
Date dateCreated
Date lastUpdated
String tag
// Relation
Tagtype tagtype
// Relation
Customer customer
// Relation
Person updatedByPerson
// Relation
Person createdByPerson
static constraints = {
dateCreated(nullable: true)
lastUpdated(nullable: true)
tag(blank: false,validator: {val,obj ->
regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
})
tagtype(nullable: true)
customer(nullable: true)
updatedByPerson(nullable: true)
createdByPerson(nullable: true)
}
String toString() {
return "${tag}"
}
}
When the constraint gets executed I get this error:
2009-08-24 18:50:53,562 [http-8080-1] ERROR errors.GrailsExceptionResolver - groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
约束闭包是静态的,因此它看不到实例字段“regExpManagerService”。但是您已经验证了该对象,因此您可以从中访问它:
The constraints closure is static, so it can't see the instance field 'regExpManagerService'. But you have the object being validated so you can access it from that: