spring - GORM无法从JAR引导

标签 spring hibernate grails groovy gorm

使用Spring,Hibernate和GORM用JAR文件替换旧版应用程序中的持久层。当使用Gradle等在项目中再次运行项目时,诸如person.save()之类的方法可以正常工作。但是,在构建胖 jar 并用-cp my-big-fat-gorm.jar引用它之后,我得到了:

java.lang.IllegalStateException: Method on class [blah.Person] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.



将Spring,Hibernate4和GORM和build.gradle文件用于Spring引导如下所示...
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'application'

mainClassName = "blah.App"

jar {
    baseName = 'blah-gorm'
    version =  '1.0-SNAPSHOT'
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.8.2'
    compile 'org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE'
    compile 'org.slf4j:slf4j-simple:1.7.7'
    runtime 'com.h2database:h2:1.4.181'
}

我是否在JAR文件创建中缺少某些东西,导致Spring启动采用@Entity等?

这是一个GitHub项目,说明了这一点,应该可以让您执行并看到与我所见相同的内容。

https://github.com/twcrone/spring-gorm-jar

最佳答案

您没有安装Spring Boot Gradle插件,因此您实际上并没有创建一个胖JAR,需要将以下内容添加到build.gradle文件中:

apply plugin: 'spring-boot'

buildscript {
    ext {
        springBootVersion = '1.1.0.M2'
        groovyVersion = '2.3.2'
    }
    repositories {
        mavenCentral()
        maven {
            url 'http://repo.spring.io/milestone'
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

这样做后,先执行gradle assemble,然后再执行java -jar ...,可以正确引导GORM

关于spring - GORM无法从JAR引导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25797317/

相关文章:

Android Volley 请求 - 将对象发送到 Spring boot java web 服务

java - 不同持久化上下文中的不同 hashCode()?

google-app-engine - Grails,App Engine,JPA-初学者在Grails上遇到麻烦

grails - 如何在grails 3中获取数据库连接

database - chalice : start the application without connecting to database

java - Spring Batch 自定义编写器 close 方法被调用两次(需要特殊处理)

java - 在测试中使用 ConfigurationProperties

java - Oauth 2.0 - 单个资源服务器但多个客户端应用程序

java - 如何知道 Hibernate 中原生 SQL 查询结果的类型?

java - 当我使用 hibernate 类在 MySQL 中选择一行时,它会自动进行更新