使用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/