grails - Grails脚本调用我自己的Java类

我刚刚创建了grails脚本来创建数据库,但是代码是用Java编写的,我认为我可以在grails脚本中使用它。

这是我的剧本

导入com.test.database。*
导入com.test.constant。*
导入org.neo4j.kernel。*

target(main:“脚本的描述在这里!”){
db = DataRepository.getInstance(new EmbeddedGraphDatabase(
Constant.PROJECT_PATH +“/ web-app / WEB-INF / resources / db”))

}

setDefaultTarget(main)

我的Java类位于src / java中,但是当我运行脚本时遇到了此错误

执行脚本DbCreate时出错:无此类属性:类DbCreate的DataRepository
没有这样的属性:类DbCreate的DataRepository
在gant.Gant $ _dispatch_closure5.doCall(Gant.groovy:387)
在gant.Gant $ _dispatch_closure7.doCall(Gant.groovy:415)
在gant.Gant $ _dispatch_closure7.doCall(Gant.groovy)
在gant.Gant.withBuildListeners(Gant.groovy:427)
在gant.Gant.this $ 2 $ withBuildListeners(Gant.groovy)
at gant.Gant $ this $ 2 $ withBuildListeners.callCurrent(未知来源)
在gant.Gant.dispatch(Gant.groovy:415)
在gant.Gant.this $ 2 $ dispatch(Gant.groovy)
在gant.Gant.invokeMethod(Gant.groovy)
在gant.Gant.executeTargets(Gant.groovy:590)
在gant.Gant.executeTargets(Gant.groovy:589)
原因:groovy.lang.MissingPropertyException:无此类属性:类DbCreate的DataRepository
在DbCreate $ _run_closure1.doCall(DbCreate:11)
在gant.Gant $ _dispatch_closure5.doCall(Gant.groovy:381)

我的问题是我可以在grails脚本中调用用户定义的Java类吗?

最佳答案

您可以像这样使用classLoader直接加载类...

def dataRepo = classLoader.loadClass('path.to.DataRepository') 

或者您也可以使用...
includeTargets << grailsScript('_GrailsBootstrap') 

应该加载所有成员。

https://stackoverflow.com/questions/6623394/

相关文章:

regex - 常规正则表达式测试失败

grails - 部署捆绑软件并从Spring EBR获得依赖捆绑软件

hibernate - Grails中的事务提交事件处理程序

json - Grails模型和子模型的JSON响应(一对多)

grails - 如果URL不包含额外路径,如何显示身份验证页面?

grails - 与Grails的零对多关系?

list - 如何在Grails中过滤列表以仅显示特定用户的 'belongsTo'?

grails - grails复合ID无效

grails - Grails:withFormat在 Action 方法中间

grails - Grails安全过滤器和/index.gsp