[英]groovy closure parameters
以下使用grails邮件插件提供的sendMail方法的示例出现在本书中 。
sendMail {
to "foo@example.org"
subject "Registration Complete"
body view:"/foo/bar", model:[user:new User()]
}
我理解{}中的代码是一个闭包,它作为参数传递给sendMail。 我也明白to
, subject
和body
的方法调用。
我试图找出实现sendMail方法的代码是什么样的,我最好的猜测是这样的:
MailService {
String subject
String recipient
String view
def model
sendMail(closure) {
closure.call()
// Code to send the mail now that all the
// various properties have been set
}
to(recipient) {
this.recipient = recipient
}
subject(subject) {
this.subject = subject;
}
body(view, model) {
this.view = view
this.model = model
}
}
这是合理的,还是我错过了什么? 特别是,在闭包( to
, subject
, body
)中调用的方法是否必须与sendMail
属于同一类的sendMail
?
谢谢,唐
MailService.sendMail关闭委托:
MailMessage sendMail(Closure callable) {
def messageBuilder = new MailMessageBuilder(this, mailSender)
callable.delegate = messageBuilder
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.call()
def message = messageBuilder.createMessage()
initMessage(message)
sendMail message
return message
}
例如,MailMessageBuilder的方法:
void to(recip) {
if(recip) {
if (ConfigurationHolder.config.grails.mail.overrideAddress)
recip = ConfigurationHolder.config.grails.mail.overrideAddress
getMessage().setTo([recip.toString()] as String[])
}
}
我不确定sendMail方法究竟是什么,因为我没有你提到的那本书。 sendMail方法确实在您描述时采用了闭包,但它可能使用构建器而不是以正常方式执行。 基本上,这将是用于描述要发送的电子邮件的域特定语言。
您定义的类不起作用的原因是闭包的范围是声明它不在其运行的位置。 因此,让闭包调用to()方法,除非将邮件服务实例传递给闭包,否则它将无法在MailService中调用to方法。
通过一些修改,您的示例可以使用常规闭包。 呼叫的以下更改和
// The it-> can be omitted but I put it in here so you can see the parameter
service.sendMail {it->
it.to "foo@example.org"
it.subject "Registration Complete"
it.body view:"/foo/bar", model:[user:new User()]
}
类中的sendMail方法应该如下所示
def sendMail(closure) {
closure(this)
// Code to send the mail now that all the
// various properties have been set
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.