【问题标题】:Reloading Grails Bootstrap with environments使用环境重新加载 Grails Bootstrap
【发布时间】:2016-10-27 09:37:00
【问题描述】:

Reloading bootstrap with Grails 中重新加载 Grails Bootstrap 有一个很好的答案

但是我在我的 init 闭包中定义了环境,所以我得到了错误:

groovy.lang.MissingMethodException: No signature of method: BootStrap.environments() is applicable for argument types: (BootStrap$_closure1_closure3) values: [BootStrap$_closure1_closure3@19ad0326]

引导代码基本上是角色、用户、用户角色的 spring-security 域类

import org.mine.*

class BootStrap
{
    def init =
    { servletContext ->
        environments
          {
            development
            {
            def adminRole = new DummyRole(authority: 'ROLE_ADMIN').save(flush: true)
            def userRole = new DummyRole(authority: 'ROLE_USER').save(flush: true)

            def user = new DummyUser(username: 'user1', email_address: 'user1@mine.org', enabled: true, password: 'password')
            def user1 = new DummyUser(username: 'user2', email_address: 'user2@mine.org', enabled: true, password: 'password')
            def user2 = new DummyUser(username: 'user3', email_address: 'user3@mine.org', enabled: true, password: 'password')

            user.save(flush: true)
            user1.save(flush: true)
            user2.save(flush: true)

            DummyUserDummyRole.create manager, adminRole, true
            DummyUserDummyRole.create user, userRole, true
            DummyUserDummyRole.create user1, userRole, true
            DummyUserDummyRole.create user2, userRole, true

            assert DummyUser.count() >= 9
            assert DummyRole.count() >= 10
            assert DummyUserDummyRole.count() >= 9


        }   // end-development
        test {

            // bootstrap data for test environment

        }
        production {

            // bootstrap data for production environment

        }
    }
}

    def destroy =
    {
            // code here
    }
}

【问题讨论】:

  • 你能发布你的引导代码吗?

标签: grails


【解决方案1】:

这对我有用:

def servletCtx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext
def myBootstrapArtefact = grailsApplication.getArtefacts('Bootstrap')[-1]
BootStrap.metaClass.environments = grails.util.Environment.&executeForCurrentEnvironment
myBootstrapArtefact.referenceInstance.init(servletCtx)

从您引用的答案中精心复制(然后修改):-)

【讨论】:

    【解决方案2】:

    我一直使用这个解决方案,它对我有用。也适用于 Grails 3.x。 Here

    【讨论】:

      【解决方案3】:

      只需将语法交换为:

      Environment.executeForCurrentEnvironment {
          production {
              // do something in production
          }
          development {
              // do something only in development
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-25
        • 2021-11-29
        • 2015-05-25
        • 2012-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        相关资源
        最近更新 更多