如何从jar将域类导入Micronaut项目? [英] How to import domain classes from jar into a Micronaut project?

查看:140
本文介绍了如何从jar将域类导入Micronaut项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置为使用GORM和Groovy(1)的Micronaut项目.该项目包含许多运行良好的域类,可以按预期将数据持久存储在MySQL数据库中.

现在,我希望使该域类对另一个Micronaut项目(2)通用.

我尝试构建仅包含域包的JAR文件,并通过build.gradle将其包含在项目(2)中.这些类已编译并可以在代码中访问,并且我可以调用诸如findBycreateCriteria等的GORM方法.还很值得一提的是,所有项目2的域类都用@Entity注释.

但是,当我运行项目(使用IntelliJ想法)并点击其中一些代码时,我得到了:

Either class [com.project2.domain.Foo] is not a 
domain class or GORM has not been initialized correctly or has already 
been shutdown. Ensure GORM is loaded and configured correctly before 
calling any methods on a GORM entity.

我知道GORM的配置和初始化正确,因为我在项目2中创建了一个本地"域类,并且一切正常.

在问这个问题之前,我看到了这个问题,也许可以带来一些启发:

提前谢谢!

我正在使用类似的设置.

我有一个具有GORM独立版本的核心库,并通过gradle的compile project()compile dep:from-artifactory:0.1-SNAPSHOT指令使用了这两个(Vert.x)顶点和Grails应用程序.

为了使其成为可能,我需要:

1)确保每个域类都用grails.gorm.annotation.Entity

注释

2)像这样调整Grails的Application.groovy:

class Application extends GrailsAutoConfiguration {

  @Override
  protected boolean limitScanningToApplication() {
    false
  }

  @Override
  Collection<String> packageNames() {
    super.packageNames() + 'your.domainclass.package'
  }

}

3)对于no-Grails项目,我需要自己查找和初始化域类,并且对于每个数据库,它可能是不同的(我的是Mongo-GORM)

ClassPathScanningCandidateComponentProvider compProvider = new ClassPathScanningCandidateComponentProvider( false )
compProvider.addIncludeFilter new AnnotationTypeFilter( Entity )
def domainClasses = compProvider.findCandidateComponents( 'io.my.domain' ).collect{ BeanDefinition bd -> Class.forName bd.beanClassName }
new MongoDatastore( config, *domainClasses )

I have a Micronaut project configured to use GORM and Groovy (1). This project contains lots of domain classes that are working perfectly, persisting data in a MySQL database as expected.

Now I wish to make this domain classes common to another Micronaut project (2).

I tried building a JAR file containing only the domain package and including it in the project (2) through build.gradle. The classes are compiled and made accessible in code, and I'm able to call GORM methods like findBy, createCriteria, etc. Is also good to mention that all project 2's domain classes are annotated with @Entity.

But, when I run the project (using IntelliJ idea) and hit some of this code I get:

Either class [com.project2.domain.Foo] is not a 
domain class or GORM has not been initialized correctly or has already 
been shutdown. Ensure GORM is loaded and configured correctly before 
calling any methods on a GORM entity.

I know that GORM is well configured and initialized because I created a "local" domain class inside project 2 and things worked fine.

Before asking this question I saw this one and maybe it can bring some inspiration: Importing domain classes from GORM-standalone module into Grails

EDIT 1: Current build.gradle dependencies:

dependencies {
    compile "io.micronaut.configuration:micronaut-kafka"
    compile "io.micronaut.configuration:micronaut-hibernate-validator"

    compile("io.micronaut.configuration:micronaut-hibernate-gorm") {
        exclude group: 'org.grails', module: 'grails-datastore-gorm-hibernate5'
    }
    compile "org.grails:grails-datastore-gorm-hibernate5:6.1.9.RELEASE"

    compile "io.micronaut:micronaut-http-client"
    compile "io.micronaut:micronaut-http-server-netty"
    compile "io.micronaut:micronaut-runtime-groovy"
    compile "io.micronaut:micronaut-validation"
    compileOnly "io.micronaut:micronaut-inject-groovy"
    runtime "ch.qos.logback:logback-classic:1.2.3"

    runtime "org.apache.tomcat:tomcat-jdbc"

    runtime "mysql:mysql-connector-java:6.0.6"

    testCompile "io.micronaut:micronaut-inject-groovy"

    testCompile("org.spockframework:spock-core") {
        exclude group: "org.codehaus.groovy", module: "groovy-all"
    }

    compile files('src/main/resources/libs/my-domain-lib.jar')
}

Thanks in advance!

解决方案

I'm using a similar setup.

I have a core-lib with GORM-standalone, and couple of (Vert.x) verticles and Grails apps using those via gradle's compile project() or compile dep:from-artifactory:0.1-SNAPSHOT directives.

In order to make it possible I needed to:

1) Make sure that each domain class is annotated with grails.gorm.annotation.Entity

2) Tweak Grails' Application.groovy like that:

class Application extends GrailsAutoConfiguration {

  @Override
  protected boolean limitScanningToApplication() {
    false
  }

  @Override
  Collection<String> packageNames() {
    super.packageNames() + 'your.domainclass.package'
  }

}

3) For no-Grails projects I needed to find and initialize the domain classes myself, and for each DB it can be different (mine is for Mongo-GORM)

ClassPathScanningCandidateComponentProvider compProvider = new ClassPathScanningCandidateComponentProvider( false )
compProvider.addIncludeFilter new AnnotationTypeFilter( Entity )
def domainClasses = compProvider.findCandidateComponents( 'io.my.domain' ).collect{ BeanDefinition bd -> Class.forName bd.beanClassName }
new MongoDatastore( config, *domainClasses )

这篇关于如何从jar将域类导入Micronaut项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆