【问题标题】:cant find SpringBeans.groovy in resources folder but i can find log4j.properties - any idea why it wont find file?在资源文件夹中找不到 SpringBeans.groovy 但我可以找到 log4j.properties - 知道为什么它找不到文件吗?
【发布时间】:2014-05-11 11:22:19
【问题描述】:

我有一个简单的 spring 文件,里面有一些示例代码

static main(args) {
    // Load the spring beans
    BeanBuilder beanBuilder = new BeanBuilder()
    //beanBuilder.loadBeans("classpath:SpringBeans.groovy")
    beanBuilder.loadBeans("file:src/main/resources/SpringBeans.groovy")
def res = new ClassPathResource ("log4j.properties")
    File fprop = res.getFile()
    assert fprop.exists()

对于 beanBuilder.loadBeans,我不得不使用固定的绝对文件 URI。我试图读取同一文件夹中的 log4j.properties 并且使用 ClassPathResource 工作 - 并且断言很好

但如果我尝试这个寻找 SpringBeans.groovy 文件

def res = new ClassPathResource ("SpringBeans.groovy")  
File fprop = res.getFile()
assert fprop.exists()

也在资源文件夹的根目录中 - 断言失败。

要让 loadBeans 工作,我必须放入一个绝对文件:URI 并且我想使用类路径形式 - 但是找不到它。

为什么我可以看到 log4j.properties 而看不到 SpringBeans.groovy 文件?

感谢您的帮助

我已将以下内容添加到我的 gradle 构建文件中,以确保获得资源目录。并且 eclipse 显示资源目录也包含在enter code here

sourceSets {
    main {
        groovy {
            srcDir 'src/main/groovy'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
}

【问题讨论】:

    标签: spring groovy resources classpath


    【解决方案1】:

    虽然 Eclipse 会将 log4j.properties 文件视为资源并简单地将其复制到输出文件夹,但 SpringBeans.groovy 将被编译,您应该会找到 SpringBeans.class 在输出文件夹而不是 .groovy 文件中。
    Eclipse 允许您定义 Groovy 脚本文件夹。与定义的模式匹配的 Groovy 文件不会被编译并复制到输出文件夹。
    要配置 Groovy 脚本文件夹,请转到 Preferences -> Groovy -> Compiler 并选中“启用脚本文件夹支持”。您还可以从项目属性中定义项目特定的行为。

    【讨论】:

    • 太棒了 - 谢谢 - 我看过那个部分,但并没有真正意识到它做了什么,所以一直保持原样。启用它并在 *.groovy 上留下复选标记,然后重新构建。并且一直保持脚本被放入输出 bin 目录非常感谢
    猜你喜欢
    • 2017-10-17
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多