Grails插件bean的覆盖方法 [英] override method of Grails plugin bean

查看:12
本文介绍了Grails插件bean的覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Security 插件提供了一个名为springSecurityService"的 grails.plugins.springsecurity.SpringSecurityService 类型的 bean.我需要覆盖此服务的 getCurrentUser 方法.

The Spring Security plugin provides a bean named 'springSecurityService' of type grails.plugins.springsecurity.SpringSecurityService. I need to override the getCurrentUser method of this service.

我第一次尝试使用扩展程序

I first tried to do it using extension

class CustomSecurityService extends SpringSecurityService {

    @Override
    Object getCurrentUser() {
        // my implementation uses methods from the parent class
    }
}

要将插件定义的 bean 替换为上述类的实例,我将以下内容添加到 resources.groovy

To replace the bean defined by the plugin with an instance of the class above I added the following to resources.groovy

springSecurityService(CustomSpringSecurityService)

但这不起作用,因为 SpringSecurityService(我正在扩展的类)的任何依赖项都没有设置,所以我得到了 NullPointerExceptions.未设置这些依赖项的原因是不再有 SpringSecurityService

But this didn't work because none of the dependencies of SpringSecurityService (the class I'm extending) are set so I get NullPointerExceptions. The reason these dependencies are not set is because there's no longer a spring bean of type SpringSecurityService

于是,我转向委托:

import grails.plugins.springsecurity.SpringSecurityService as PluginSpringSecurityService

class CustomSpringSecurityService {

    @Autowired @Delegate
    PluginSpringSecurityService pluginSpringSecurityService

    Object getCurrentUser() {
        // my implementation uses methods from pluginSpringSecurityService
    }
}

然后我在 resources.groovy

springSecurityService(CustomSpringSecurityService)
pluginSpringSecurityService(grails.plugins.springsecurity.SpringSecurityService)

在第二次尝试中,我再次希望名为springSecurityService"的 bean 引用 CustomSpringSecurityService,但我还需要一个 grails.plugins.springsecurity.SpringSecurityService 类型的 bean>,因为我对 getCurrentUser 的实现使用了该 bean 的其他一些方法.

In this second attempt, I again want the bean named 'springSecurityService' to refer to CustomSpringSecurityService, but I also need a bean of type grails.plugins.springsecurity.SpringSecurityService, because my implememtation of getCurrentUser use some other methods of that bean.

然而,我再次发现pluginSpringSecurityService的依赖没有被设置.是否有更简单的方法来覆盖受依赖注入影响的上下文中插件提供的 bean 的方法?

However, I again found that the dependencies of pluginSpringSecurityService are not being set. Is there an easier way to override a method of a bean provided by a plugin in a context that is subject to dependency injection?

推荐答案

回到子类化和重新定义 resources.groovy 中的 bean,但满足依赖关系.它们按名称自动注入,但全部列出,因此将它们明确添加到您的重新定义中:

Go back to subclassing and redefining the bean in resources.groovy, but satisfy the dependencies. They're auto-injected by name but all listed, so add them explicitly to your redefinition:

springSecurityService(CustomSpringSecurityService) {
   authenticationTrustResolver = ref('authenticationTrustResolver')
   grailsApplication = ref('grailsApplication')
   passwordEncoder = ref('passwordEncoder')
   objectDefinitionSource = ref('objectDefinitionSource')
   userDetailsService = ref('userDetailsService')
   userCache = ref('userCache')
}

这篇关于Grails插件bean的覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆