【问题标题】:Grails 3 - Read classpath resource in Bootstrap.groovyGrails 3 - 在 Bootstrap.groovy 中读取类路径资源
【发布时间】:2016-03-05 06:38:00
【问题描述】:

我正在将 Grails 2.5 应用程序迁移到 Grails 3.0.9,但在读取 grails-app/init/BootStrap.groovy 中的类路径资源时遇到了一些问题。

在 Grails 2.5.x 中,当将资源文件放入与 BootStrap.groovy 相同的文件夹时,可以使用 Spring ClassPathResource 访问资源(例如文本文件)

def file1 = new ClassPathResource('my-textfile.txt').file

在 Grails 3 中,当我使用 grails run-app 启动我的应用程序时,它可以工作。所有资源均取自src/main/resources。但是,如果我使用 IntelliJ 中的 Spring Boot Application(位于 /grails-app/init/a/b/Application.groovy)类启动应用程序,BootStrap.groovy 将不再找到资源。

在使用Application.groovy 启动应用程序时,有谁知道如何在 Grails 3.0.9 中的BootStrap.groovy 中访问这些文件?

【问题讨论】:

    标签: grails grails-3.0 grails-3.0.9


    【解决方案1】:

    根据 Gradle 约定,我将资源放在 src/main/resource 下,因为它们需要在运行时可访问。

    由于某些原因,我的grails-app/init/a/b/Application.groovy 找不到这些资源,所以我不得不放置一个指向grails-app/init/a/b 的符号链接。之后一切都按预期工作。

    【讨论】:

      【解决方案2】:
      class BootStrap {
      
           def init = { servletContext ->
      
              def config = new ConfigSlurper().parse(new File("${System.properties['user.dir']}/grails-app/conf/test.groovy").toURL())
              println config
              println config.hfy.test
          }
          def destroy = {
          }
      }
      

      ${System.properties['user.dir']} : 项目目录

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-27
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 1970-01-01
        • 2010-12-26
        相关资源
        最近更新 更多