简体   繁体   中英

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. 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 . 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. Note that where I have to wire it into is a Java class.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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