简体   繁体   English

Grails:直接从src / java使用GORM的最佳方法是什么?

[英]Grails: what is the best way to use GORM directly from src/java?

In my grails application I have Java classes (src/java). 在我的grails应用程序中,我有Java类(src / java)。 And I want to have access to my domain classes and use GORM features(like get(), findBy..., save(), delete() and etc.) directly from my Java classes. 我希望能够访问我的域类,并直接从我的Java类中使用GORM功能(如get(),findBy ...,save(),delete()等)。 I know, I can do this by Spring IoC: for example, I can add grails service to my Java class: 我知道,我可以通过Spring IoC来做到这一点:例如,我可以在我的Java类中添加grails服务:

public class SimpleJavaClass{

    //...    

    @Autowired
    private ExampleService exampleService;

    //...
}

And wireup each instance of this class by Spring: 并通过Spring连接此类的每个实例:

//...

GrailsApplication grailsApplication

//...

def simpleAction(){
    def instance = new SimpleJavaClass()
    grailsApplication.mainContext.autowireCapableBeanFactory.autowireBean(instance)
}

But may be there is more appropriate way to do same? 但是可能有更合适的方法来做同样的事情吗?

Using Grails 2.0, the only current way is to package your domain classes into a binary plugin (see http://grails.org/doc/2.0.x/guide/single.html#binaryPlugins ) 使用Grails 2.0,目前唯一的方法是将您的域类打包成二进制插件(请参阅http://grails.org/doc/2.0.x/guide/single.html#binaryPlugins

You then can depend on this binary plugin and because it is precompiled the Java code will see many of the GORM methods which are wired into the byte code 然后,你可以依赖这个二进制插件,因为它是预编译的,Java代码将看到许多连接到字节代码的GORM方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM