是否可以在我的 grails 应用程序上自定义 hibernate 插件自动生成的 CRUD 方法?
我的项目副本在 HibernateGrailsPlugin.groovy 中包含以下代码片段:
def doWithDynamicMethods = {
def dynamicMethods = HibernatePluginSupport.doWithDynamicMethods
dynamicMethods.delegate = delegate
dynamicMethods.call(it)
// aids in generating appropriate documentation in plugin.xml since
// domain class methods are lazily loaded we initialize them here
if(plugin.basePlugin) {
try {
def clz = application.classLoader.loadClass("org.grails.Behavior")
clz.count()
}
catch(e) {
// ignore
}
}
}
我是 grails/groovy 的新手,但如果我理解正确的话,此闭包将动态方法的添加/删除委托给 HibernatePluginSupport 类的 doWithDynamicMethods 闭包。 看来 HibernatePluginSupport 类是用 Grails 特定代码的其余部分编译的,如果 HibernatePluginSupport 的代码位于我项目的插件副本中,我可以轻松地根据我的需要对其进行自定义。 我的问题是,如何修改特定类的自动生成方法?我看到的唯一方法是在 groovy 文件中重写 doWithDynamicMethods 闭包,但我不想这样做,因为我只是为某些域类自定义它。更具体地说,如何删除/替换“HibernatePluginSupport.doWithDynamicMethods”闭包添加的动态方法?
My project's copy contains the following snippet in HibernateGrailsPlugin.groovy :
def doWithDynamicMethods = {
def dynamicMethods = HibernatePluginSupport.doWithDynamicMethods
dynamicMethods.delegate = delegate
dynamicMethods.call(it)
// aids in generating appropriate documentation in plugin.xml since
// domain class methods are lazily loaded we initialize them here
if(plugin.basePlugin) {
try {
def clz = application.classLoader.loadClass("org.grails.Behavior")
clz.count()
}
catch(e) {
// ignore
}
}
}
I'm new to grails/groovy, but if I understand correctly, this closure is delegating the add/removal of dynamic methods to the doWithDynamicMethods closure of the HibernatePluginSupport class.
It seems the HibernatePluginSupport class is compiled with the rest of grails specific code, if the code for HibernatedPluginSupport lived in my project's copy of the plugin I could easily customize it to my needs.
My question is, how do I modify the autogenerated methods for specific classes? The only way I see is rewriting the doWithDynamicMethods closure in the groovy file, but I don't want to do that since I'm only customizing it for some domain classes. More specifically, How do I remove/replace dynamic methods added by the 'HibernatePluginSupport.doWithDynamicMethods' closure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:动态查找器基于 Groovy 的元编程功能。
您不应修改 HibernateGrailsPlugin.groovy。该文件是 Grails Hibernate 插件的一部分,在执行某些 Grails 命令时将被覆盖。
由于您可以随时在应用程序中自由修改 Groovy 对象元类,因此您可以
a) 创建自己的 grails 插件,该插件潜入自定义动态查找器(依赖于 Hibernate Grails 插件)或
b) 抛出您的元-对 Bootstrap.groovy 进行类修改以在应用程序启动时应用它们
first of all: dynamic finders are based on Groovy's meta-programming features.
You should not modify the HibernateGrailsPlugin.groovy. This file is part of the Grails Hibernate plugin and will be overridden when executing certain Grails commands.
Since you are free to modify your Groovy objects meta-classes at any time from within your application you could
a) create your own grails plugin that sneaks in custom dynamic finders (with a dependency on the Hibernate Grails plugin) or
b) throw your meta-class modifications into Bootstrap.groovy to apply them on application startup