【问题标题】:Grails config of Spring beans in different files不同文件中 Spring bean 的 Grails 配置
【发布时间】:2010-02-16 22:21:51
【问题描述】:

Grails 有用于 spring bean 的配置,称为 resources.groovy。正如我从文档中了解的那样,它允许您使用 loadBeans(%path%) 包含另一个文件

我试过这个:

println 'loading application config ...'


// Place your Spring DSL code here
beans = {
    loadBeans("classpath:security") //i'm tried with "spring/security" and "spring/security.groovy" also

}

但是当 grails 运行时,它会记录以下错误:

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Error evaluating bean definition script: class path resource [security] cannot be opened because it does not exist
Offending resource: class path resource [security]; nested exception is java.io.FileNotFoundException: class path resource [security] cannot be opened because it does not exist
 at grails.spring.BeanBuilder.loadBeans(BeanBuilder.java:470)
 at grails.spring.BeanBuilder.loadBeans(BeanBuilder.java:424)
 at resources$_run_closure1.doCall(resources.groovy:13)
 at resources$_run_closure1.doCall(resources.groovy)
 ... 45 more

脚本security.groovy 存在于grails-app/conf/spring 并由grails maven 插件 编译成target/classes/security.class。 此时目录target/resources/spring为空

如何配置 Grails 或 grails-maven-plugin 以复制此配置文件,而不是将其编译成类?

附言当我尝试在 conf/Config.groovy 中包含使用 grails.config.locations = [ %path% ] 的配置脚本时,也会出现这个问题,我的 groovy 脚本编译成类,因此,grails config builder 找不到它们:(

【问题讨论】:

    标签: java grails groovy maven config


    【解决方案1】:

    你试过了吗:

    println 'loading application config ...'
    
    
    // Place your Spring DSL code here
    beans = {
        loadBeans("classpath:*security.groovy") 
    
    }
    

    (这应该加载类路径上以security.groovy 结尾的所有Groovy 文件并将它们解析为bean 定义)。

    更新: 找到一个interesting threadthis message 作为参考,我的理解是一个技巧是在scripts/_Events.groovy 中使用ant 将.groovy 文件复制到classesDirPath dir 然后简单地使用:

    beans = {
        // load spring-beans for db-access via spring-jdbc-template
        loadBeans('security.groovy')
    
        // load some other spring-beans
            ...
    }
    

    这看起来像是在战争和运行 run-app 时让事情正常工作的 hack。不确定事情“应该”如何完成(如果这有意义的话)。

    【讨论】:

    • 试过了,但没有成功:(而且类路径中的任何地方都没有任何security.groovy,grails将它编译成security.class 我认为这是问题的根源,但不知道如何解决这个问题
    • @Pascal Thivent 你能重新发布/更新 Nabble 的链接吗?他们似乎不见了……
    • 我在 _Event 中尝试了以下操作,并且成功了。 eventCompileEnd = { ant.copy(file: "${basedir}/grails-app/conf/spring/security.groovy", todir: classesDirPath) }
    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多