创建后无法运行 grails 插件。 Get ERROR os.boot.SpringApplication 应用程序启动失败

Posted

技术标签:

【中文标题】创建后无法运行 grails 插件。 Get ERROR os.boot.SpringApplication 应用程序启动失败【英文标题】:Cannot run grails plugin after creation. Get ERROR o.s.boot.SpringApplication Application startup failed 【发布时间】:2017-09-01 14:12:08 【问题描述】:

我很难弄清楚为什么 Grails 无法运行我创建的插件。这是我的环境:

Grails 版本:3.2.6 Groovy 版本:2.4.7 JVM 版本:1.8.0_91

这些是我的步骤:

    我创建了插件:

    grails create-plugin bioprofile
    

    我添加 Spring Security 核心插件,将此行添加到 build.gradle:

    compile 'org.grails.plugins:spring-security-core:3.1.1'
    

    我运行s2-quickstart 命令来设置UserRoleUserRole

    grails s2-quickstart cscie56.ps5 User Role
    

    我修改了User 域类以包含一些新字段

    创建了一些其他领域类

    grails create-domain-class cscie56.ps5.BlogEntry
    grails create-domain-class cscie56.ps5.Comment
    

    我生成了控制器等...

    grails generate-all BlogEntry
    grails generate-all User
    grails generate-all Role
    grails generate-all UserRole
    

    我在BootStrap.groovy文件中添加了如下初始化:

    package bioprofile
    
    import cscie56.ps5.Role
    import cscie56.ps5.User
    import cscie56.ps5.UserRole
    
    class BootStrap 
    
        def init =  servletContext ->
            environments 
                development 
                    setupData()
                    setupUsersAndRoles()
                    println "Developement execution"
                
                test 
                    setupData()
                    setupUsersAndRoles()
                    println "Test execution"
                
                production 
                    // do nothing
                    println "Production execution"
                
            
        
    
        def destroy = 
        
    
        def setupUsersAndRoles() 
            User admin = new User(username: 'admin', password: 'password')
            admin.save(flush: true)
            User user = new User(username: 'user', password: 'user')
            user.save(flsuh:true)
    
            Role adminRole = new Role(authority: Role.ROLE_ADMIN)
            adminRole.save(flush:true)
    
            Role userRole = new Role(authority: Role.ROLE_USER)
            userRole.save(flush:true)
    
            UserRole.create(admin, adminRole)
            UserRole.create(admin, userRole)
            UserRole.create(user, userRole)
        
    
        def setupData() 
    
        
    
    

当我使用 grails run-app 运行应用程序时,我收到了这个乏味的错误:

Running application...
objc[84720]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.

Configuring Spring Security Core ...
... finished configuring Spring Security Core

2017-04-06 06:20:06.032 ERROR --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Either class [cscie56.ps5.User] 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.
    at org.grails.datastore.gorm.GormEnhancer.stateException(GormEnhancer.groovy:387)
    at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:273)
    at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:270)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormInstanceApi(GormEntity.groovy:1326)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:151)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper$save.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at cscie56.ps5.User.save(User.groovy)
    at cscie56.ps5.User.save(User.groovy)
    at org.grails.datastore.gorm.GormEntity$save.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at bioprofile.BootStrap.setupUsersAndRoles(BootStrap.groovy:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:158)
    at bioprofile.BootStrap$_closure1$_closure3$_closure4.doCall(BootStrap.groovy:13)
    at bioprofile.BootStrap$_closure1$_closure3$_closure4.doCall(BootStrap.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at groovy.lang.Closure.call(Closure.java:414)
    at bioprofile.BootStrap$_closure1$_closure3$_closure4.call(BootStrap.groovy)
    at groovy.lang.Closure.call(Closure.java:408)
    at bioprofile.BootStrap$_closure1$_closure3$_closure4.call(BootStrap.groovy)
    at grails.util.Environment$EnvironmentBlockEvaluator.execute(Environment.java:529)
    at grails.util.Environment.executeForEnvironment(Environment.java:510)
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:485)
    at org.grails.web.servlet.boostrap.DefaultGrailsBootstrapClass.callInit(DefaultGrailsBootstrapClass.java:62)
    at org.grails.web.servlet.context.GrailsConfigUtils.executeGrailsBootstraps(GrailsConfigUtils.java:65)
    at org.grails.plugins.web.servlet.context.BootStrapClassRunner.onStartup(BootStrapClassRunner.groovy:53)
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy:256)
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
    at grails.boot.GrailsApp$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at bioprofile.Application.main(Application.groovy:10)

我试着跑了

grails clean
grails clean --refresh-dependencies
grails compile

但结果相同。

有人遇到过这个问题或知道根本原因是什么?

【问题讨论】:

问题(我仍然不明白根本原因)似乎与以下指令有关: admin.save(flush: true) adminRole.save(flush:true) userRole.save( flush:true) ...所以某些东西可能没有正确初始化以允许持久性发生? .withTransaction @vahid 你能提供更多细节吗? 这是一个重复的主题,您是否对该词进行了任何搜索。因为如果你确实很确定会返回很多结果。 grails 中的服务在 grails 3 上的任何其他地方都是自动事务的,您需要将事务包装在它周围。 类似的主题表明缺少休眠配置,但据我所知,在我的休眠 5 情况下,生成的配置样板看起来不错。这些是没有服务的域类。你能指出一个正确的例子,应该在哪里使用 .withTransaction 以及它应该实际包装什么? 【参考方案1】:

如果其他人遇到同样的问题,这对我有用。 这似乎是一个休眠依赖问题。不明显,而且很可能是在多个文件中生成样板文件的任何细微错误(从来不是“我会为你做这一切”框架的忠实粉丝)。 在我的特定情况下(这可能特定于我的环境),通过在 build.gradle 文件中用 hibernate4 替换生成的 hibernate5 依赖项,上面的错误就消失了。这就是我现在所拥有的(删除了 hibernate5 的东西):

classpath "org.grails.plugins:hibernate4:5.0.10"

compile "org.grails.plugins:hibernate4"
compile "org.hibernate:hibernate-core:4.3.10.Final"
compile "org.hibernate:hibernate-ehcache:4.3.10.Final"

.withTransaction hack 似乎对解决这个问题没有任何作用。

【讨论】:

以上是关于创建后无法运行 grails 插件。 Get ERROR os.boot.SpringApplication 应用程序启动失败的主要内容,如果未能解决你的问题,请参考以下文章

Grails 2 - 无法创建弹簧安全域对象

grails无法从插件中找到视图

无法运行 Grails 项目 GGTS

Grails 4:邮件插件在 Elastic Beanstalk 上运行时无法读取“密码”属性

Grails 无法安装插件

创建和安装 grails 插件 - 在安装期间/安装后,我的插件如何从依赖的插件访问资源?