Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Grails3中的application.yml定义服务Bean的属性?_Grails_Grails 3.0_Grails Config - Fatal编程技术网

如何从Grails3中的application.yml定义服务Bean的属性?

如何从Grails3中的application.yml定义服务Bean的属性?,grails,grails-3.0,grails-config,Grails,Grails 3.0,Grails Config,我能够从Config.groovy定义服务bean的属性 引用《圣杯2权威指南》一书 清单10-6。使用Config.groovy配置bean 这种方法的一个优点是,由于提供的功能 通过Config.groovy,您可以轻松地指定每个环境的值,而不是 而不是将值硬编码到AlbumStartService类中。有了这个 配置代码到位后,硬编码值可从表格中删除 AlbumArtService类。该属性仍然需要声明为 类中的字段,但不应指定值。框架 将负责使用指定的值初始化属性 在Config.gro

我能够从Config.groovy定义服务bean的属性

引用《圣杯2权威指南》一书

清单10-6。使用Config.groovy配置bean

这种方法的一个优点是,由于提供的功能 通过Config.groovy,您可以轻松地指定每个环境的值,而不是 而不是将值硬编码到AlbumStartService类中。有了这个 配置代码到位后,硬编码值可从表格中删除 AlbumArtService类。该属性仍然需要声明为 类中的字段,但不应指定值。框架 将负责使用指定的值初始化属性 在Config.groovy中

在Grails2中,我定义了服务bean的属性,如上所述

现在在Grails 3中,我尝试在application.ml文件中定义服务属性:

environments:
    development:
        beans:
            transactionalMailService:
                mandrillApiKey: XAPIKEYVALUEX
            shareWithShoptimixUseCaseService:
                appStore: https://itunes/myapp
        grails:
            serverURL: http://localhost:8080
        dataSource:
            driverClassName: org.postgresql.Driver
            dialect: org.hibernate.dialect.PostgreSQL9Dial

    ....
    ...
    ..
    .
然后为我服务:

class TransactionalMailService {

    def mandrillApiKey

    ....
    ...
    ..
    .
}
但未设置该属性。你知道如何在Grails 3中做到这一点吗?

我有一个解决方案,但对我来说,这仍然是一个需要解决的问题

我喜欢在Grails2中设置控制器和服务bean属性的方式。但是由于无法在Grails3中以同样的方式工作,我选择在Bootstrap.groovy中设置属性


<>我不认为这是答案,但它确实有效。

你需要把多个文档放到同一个YML文件中。

beans:
   transactionMailService:
      mandrilApiKey: real key
---
spring:
    profiles: development
beans:
      transactionMailService:
        mandrilApiKey: dev key
这(我认为)是因为bean是由spring配置的

def init = { servletContext ->
        myService.someProperty = 'some value'
...
}
beans:
   transactionMailService:
      mandrilApiKey: real key
---
spring:
    profiles: development
beans:
      transactionMailService:
        mandrilApiKey: dev key