开发者

Using a service from a non-Spring Groovy class

开发者 https://www.devze.com 2023-04-01 09:53 出处:网络
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

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

  1. pass the dependencies into the class
  2. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号