Grails 依赖注入在服务之外?

发布于 2024-12-03 17:51:16 字数 493 浏览 1 评论 0原文

我有一个 Grails 应用程序,需要运行一个可能会随着时间的推移而被替换的策略。我知道 Spring 是 Grails 的基础,所以我想知道我是否可以访问 Spring 的 IoC 容器,以便我可以在 xml 文件中外部化实际依赖项(注意:我从未真正这样做过,但只是知道它,所以我可能会缺少一些东西)。我的目标是能够执行如下操作:

class SchemaUpdateService {
public int calculateSomething(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
IStrategy strat = (IStrategy) ctx.getBean("mystrat");
}
}

然后在 beans.xml 文件中映射适当的实现。我认为 Grails 中支持这一点。有谁有关于这将如何工作的任何文档吗?我真的只需要 Spring IoC 库并且它就能工作吗?谢谢!

I have a Grails application that needs to run a strategy that will likely be swapped out over time. I know Spring underlies Grails, so I was wondering if I had access to Spring's IoC container so that I could externalize the actual dependency in an xml file (note: I have never actually done this, but just know of it, so I may be missing something). My goal is to be able to do something like the following:

class SchemaUpdateService {
public int calculateSomething(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
IStrategy strat = (IStrategy) ctx.getBean("mystrat");
}
}

And then map the appropriate implementation in the beans.xml file. I assume this is supported in Grails. Does anyone have any documentation on how this would work? Do I really just need the Spring IoC library and it will just work? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离笑几人歌 2024-12-10 17:51:16

您可以在 resources.xmlresources.groovy 中定义您的 bean。 grails 文档 非常清楚如何访问Spring应用程序上下文。

You define your beans in resources.xml or resources.groovy. The grails documentation is very clear about how to access the Spring application context.

彻夜缠绵 2024-12-10 17:51:16

您可以使用以下方法从任何 Grails 工件访问应用程序上下文

ApplicationContext ctx = grailsApplication.mainContext

然后可以使用它来检索您感兴趣的任何 bean:

IStrategy strat = (IStrategy) ctx.getBean("mystrat")

在无法访问 grailsApplication 的类中,您可以使用这样的帮助器如下所示来访问应用程序上下文和其中的 bean

class SpringUtils {

    static getBean(String name) {
        applicationContext.getBean(name)
    }

    static <T> T getBean(String name, Class<T> requiredType) {
        applicationContext.getBean(name, requiredType)
    }

    static ApplicationContext getApplicationContext() {
        ApplicationHolder.application.mainContext
    }
}

但是,只有当您需要在运行时检索同一 bean 的不同实现时,才需要这样做。如果所需的 bean 在编译时已知,只需在 resources.xmlresources.groovy 中将这些 bean 连接在一起

You can access the application context from any Grails artefact using

ApplicationContext ctx = grailsApplication.mainContext

You can then use this to retrieve whichever beans you're interested in:

IStrategy strat = (IStrategy) ctx.getBean("mystrat")

In classes that don't have access to grailsApplication, you could use a helper such as the following to access the application context and the beans therein

class SpringUtils {

    static getBean(String name) {
        applicationContext.getBean(name)
    }

    static <T> T getBean(String name, Class<T> requiredType) {
        applicationContext.getBean(name, requiredType)
    }

    static ApplicationContext getApplicationContext() {
        ApplicationHolder.application.mainContext
    }
}

However, this should only be necessary if you need to retrieve different implementations of the same bean at runtime. If the required bean is known at compile-time, just wire the beans together in resources.xml or resources.groovy

秉烛思 2024-12-10 17:51:16

首先,您要在 grails-app/conf/spring/resources.groovy 中定义策略:

beans = {
    myStrat(com.yourcompany.StrategyImpl) {
       someProperty = someValue
    }
}

然后,您只需 def 具有相同名称的属性到您的服务中:

class SomeGrailsService {
    def myStrat

    def someMethod() {
        return myStrat.doSomething()
    }
}

在任何 Grails 工件(例如服务和域类)中,Grails 都会自动为 myStrat 属性提供正确的值。但不要忘记,在单元测试中,您必须手动为其指定一个值,因为自动装配不会在单元测试中发生。

在 Grails 工件之外,您可以使用如下内容:

def myStrat = ApplicationHolder.application.mainContext.myStrat

在 Grails 2.0 中,Graeme 等人不赞成使用 *Holder 类(例如 ApplicationHolderConfigurationHolder),所以我不太确定 Grails 2.0 方法是什么......

First of all, you want to define your strategy in your grails-app/conf/spring/resources.groovy:

beans = {
    myStrat(com.yourcompany.StrategyImpl) {
       someProperty = someValue
    }
}

Then, you simply def the a property with the same name into your service:

class SomeGrailsService {
    def myStrat

    def someMethod() {
        return myStrat.doSomething()
    }
}

In any Grails artefact (such as services and domain classes), Grails will automatically give the myStrat property the correct value. But don't forget, in a unit test you'll have to give it a value manually as the auto-wiring does not happen in unit tests.

Outside of a Grails artefact, you can use something like:

def myStrat = ApplicationHolder.application.mainContext.myStrat

In Grails 2.0, Graeme et al are deprecating the use of the *Holder classes (such as ApplicationHolder and ConfigurationHolder), so I'm not quite sure what the Grails 2.0 approach would be...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文