[英]Call grails plugin closure from Groovy class
I am using the AWS plugin for grails. 我将AWS插件用于grails。 I am trying to use the sesMail closure from a Groovy class, not a service or controller eg in a method:
我正在尝试从Groovy类而不是服务或控制器中使用sesMail闭包,例如在方法中:
String msgId = sesMail {
from props.from
replyTo props.replyTo
to prop.to
html props.body
}
I am getting an error saying sesMail is not a method of the Groovy class. 我收到一个错误消息,说sesMail不是Groovy类的方法。 My questions are:
我的问题是:
How can I call a plugin closure that is ordinarily available in grails service and controllers from a regular Groovy class 如何从常规Groovy类中调用grails服务和控制器中通常可用的插件关闭
Following that I'm curious how these closures are defined and made available in the service and controller. 之后,我很好奇如何在服务和控制器中定义这些闭包并使其可用。 I can't find a definition of sesMail in the plugin.
我在插件中找不到sesMail的定义。
thanks 谢谢
Good Question. 好问题。 And I appreciate that you are curious to find the root cause of your problem.
我很高兴您很好奇地找到问题的根本原因。 Here are your answers:-
这是您的答案:-
You can directly call the SendSesMail bean to send mail instead of calling the sesMail
closure. 您可以直接调用SendSesMail bean来发送邮件,而不是调用
sesMail
闭包。 Your groovy class would look like: 您的常规类如下所示:
class MyGroovyClass{ def sendSesMail def sendSomeMails(){ String msgId = sendSesMail.send{ from props.from replyTo props.replyTo to prop.to html props.body } } }
As you can see SendSesMail is not a service class it is just a POGO, so it will not be autowired in this groovy class unless you define that in resources.groovy
. 如您所见,SendSesMail不是服务类,它只是一个POGO,因此除非您在
resources.groovy
定义它,否则它不会在此groovy类中自动装配。 So: 所以:
Something like: 就像是:
//resources.groovy
beans = {
sendSesMail(grails.plugin.aws.ses.SendSesMail)
}
Also keep a note by using the above method directly you would be bypassing grails.plugin.aws.ses.enabled
configuration. 通过直接使用上述方法也要注意,您将绕过
grails.plugin.aws.ses.enabled
配置。 So you have to handle it explicitly. 因此,您必须明确地处理它。
sesMail
method which is metaClasses over target classes to take a Closure as an argument here in MetaClassInjector . sesMail
方法的定义,它是目标类上的metaClasses,可以在此处MetaClassInjector中将Closure作为参数。 This class is basically used to inject/add dynamicMethods
from the plugin definition. dynamicMethods
。 You can find it as AwsGrailsPlugin (Line: 37) --> AwsPluginSupport (Line: 86) --> MetaClassInjector (Line: 50) AwsGrailsPlugin (第37行)-> AwsPluginSupport (第86行)-> MetaClassInjector (第50行)
You can also see in MetaClassInjector (around line 45 and 46), the target classes are controller and service classes. 您还可以在MetaClassInjector中 (第45和46行周围)看到,目标类是控制器和服务类。 Hence you find the closure
sesMail
available in those 2 artefacts. 因此,您可以在这2个
sesMail
找到sesMail
闭包。
I hope it is clear enough to address your curiousness. 我希望足够清楚可以解决您的好奇心。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.