【问题标题】:Grails instantiates object and uses in ControllersGrails 实例化对象并在控制器中使用
【发布时间】:2011-07-16 09:35:15
【问题描述】:

我有一个应用程序,它只需要实例化一个对象,我在考虑 Singleton,但在我开始之前,我在考虑依赖注入。我想做的是,在Bootstrap.groove 中将对象和setAttribute 实例化为ServletContext,并在每个控制器中使用该对象。我的理解是引导程序在应用程序生命周期内只会被调用一次?我可以这样做吗?

【问题讨论】:

    标签: grails bootstrapping


    【解决方案1】:

    你可以在 resources.groovy 中定义单例 bean

    beans = {
        myBean(my.company.MyBeanImpl) {
            singleton true
        }
    }
    

    然后在你的控制器中

    class ExampleController {
         def myBean
    }
    

    所有文档都在这里:http://www.grails.org/doc/latest/guide/14.%20Grails%20and%20Spring.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多