I am implementing a custom Interactiv开发者_StackOverflow社区eAuthenticationSuccessEventListener to react on events from the spring-security-core plugin - the source file is in /src/groovy
In this class, how can I use a service defined in grails-app/services ? Dependency-injection (obviously?) does not work.
Here are two options
- pass the dependencies into the class
- retrieve the spring bean within the class using a helper class such as
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.springframework.context.ApplicationContext
public class SpringUtils {
static getSpringBean(String name) {
getApplicationContext().getBean(name);
}
static ApplicationContext getApplicationContext() {
return ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
}
}
The first option is better IMO
精彩评论