简体   繁体   English

Grails 2.4.5如何连接对应用程序上下文的引用?

[英]Grails 2.4.5 How do I wire in an reference to an application context?

I can't believe this is so hard to figure out. 我不敢相信这是如此难以理解。 I'm a Grails / Groovy newb though and what I expect is that this supposed to be done in Groovy somehow. 我虽然是Grails / Groovy newb,但我期望这应该以某种方式在Groovy中完成。 I'm resurrecting an old project and updating it for Java 8. So far I've the libraries and dependencies fixed but one of the things I had to change was getting a reference in a Java class to org.springframework.core.env.Environment . 我正在恢复一个旧项目并针对Java 8对其进行更新。到目前为止,我已经修复了库和依赖项,但是我必须更改的一件事情是在Java类中获得对org.springframework.core.env.Environment的引用org.springframework.core.env.Environment I thought that grabbing that out of the application context would be easy but it's done in some way I don't understand. 我以为从应用程序上下文中抓取到它会很容易,但这是以我不了解的某种方式完成的。

I've been pouring over this http://docs.grails.org/2.4.5/guide/spring.html but it's still not clear. 我一直在浏览此http://docs.grails.org/2.4.5/guide/spring.html,但仍不清楚。 Note that where I have to wire it into is a Java class. 请注意,我必须将其连接到的是Java类。

If this class isn't going to be a Spring bean or in some other way a candidate for dependency injection, then it's very likely called directly or indirectly from a Spring bean, eg as a helper for a controller, or service, or Quartz job, etc. Have the caller pass dependencies (other beans, the ApplicationContext , etc.) to it in the constructor or as method args, depending on what's more convenient. 如果此类不是Spring bean或以其他方式成为依赖项注入的候选对象,那么很可能直接或间接地从Spring bean调用它,例如,作为控制器,服务或Quartz作业的助手。等等。让调用者将依赖项(其他bean, ApplicationContext等)传递给它的构造函数或方法args,这取决于更方便的方法。

If you can't get anything reasonable to work using dependency injection and/or passing along injected dependencies, you can always violate DI and IoC and use the grails.util.Holders antipattern class and pull in the context or Spring beans, but this should only be done if there's no proper solution. 如果使用依赖项注入和/或传递注入的依赖项无法获得合理的工作条件,则可以始终违反DI和IoC并使用grails.util.Holders反模式类并grails.util.Holders上下文或Spring Bean,但这应该仅在没有适当解决方案的情况下进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM