【问题标题】:How to define properties of a Service Bean from application.yml in Grails 3?如何在 Grails 3 中从 application.yml 定义服务 Bean 的属性?
【发布时间】:2015-06-03 15:34:22
【问题描述】:

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

引用Book The Definitive Guide to Grails 2

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

beans {
    albumArtService {
        artworkRequestUrl = 'http://itunes...'
    }
}

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

在 Grails 2 中,我已经定义了服务 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 中执行此操作吗?

【问题讨论】:

    标签: grails grails-3.0 grails-config


    【解决方案1】:

    我有一个solution,但对我来说这仍然是一种解决方法。

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

    def init = { servletContext ->
            myService.someProperty = 'some value'
    ...
    }
    

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

    【讨论】:

      【解决方案2】:

      您需要将多个文档放入同一个 YML 文件中。

      beans:
         transactionMailService:
            mandrilApiKey: real key
      ---
      spring:
          profiles: development
      beans:
            transactionMailService:
              mandrilApiKey: dev key
      

      这是(我认为)因为 bean 是由 spring 配置的

      【讨论】:

        猜你喜欢
        • 2020-01-25
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        相关资源
        最近更新 更多