Grails 3:覆盖 doWithSpring 中的服务
Grails 3 : overriding service in doWithSpring
在 grails 2 中,覆盖 XPlugin.groovy 中的 bean 只需要在 doWithSpring() 方法中重新定义服务:
def doWithSpring = {
tokenStorageService(TokenStorageProxyService) { it.autowire = 'byName' }
}
我开始在 Grails 3 中编程,已经有了 Grails 2 的经验,并在新插件中尝试了相同类型的覆盖,因为我正在为我现有的一些 Grails 2 插件创建 Grails 3 版本:
Closure doWithSpring() { {->
tokenStorageService(TokenStorageProxyService) {
it.autowire = 'byName'
}
} }
我做了一些集成测试:
@Integration
class SecurityServiceIntegrationSpec extends Specification {
SecurityService securityService
TokenStorageProxyService tokenStorageService
...
}
当 运行 测试时,我得到错误:
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'grails.plugin.springsecurity.rest.token.storage.jwt.JwtTokenStorageService' to required type 'com.b2boost.grails3.auth.client.TokenStorageProxyService' for property 'tokenStorageService'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'grails.plugin.springsecurity.rest.token.storage.jwt.JwtTokenStorageService' to required type 'com.b2boost.grails3.auth.client.TokenStorageProxyService' for property 'tokenStorageService': no matching editors or conversion strategy found
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:605)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:400)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
...
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'grails.plugin.springsecurity.rest.token.storage.jwt.JwtTokenStorageService' to required type 'com.b2boost.grails3.auth.client.TokenStorageProxyService' for property 'tokenStorageService': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
... 37 more
默认的 JwtTokenStorageService 似乎没有被我的 TokenStorageProxyService 覆盖,导致在将服务连接到集成测试时出现 ConversionNotSupportedException。如何覆盖我的插件中的服务?
经过一些调查,我的自定义模块似乎是在 Spring 安全休息后加载的。因此,来自 SSR 的 tokenStorageService 覆盖了我的服务,而相反的情况应该发生:
Configuring MyModule...
... finished configuring MyModule
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Configuring Spring Security REST 2.0.0.M2...
... finished configuring Spring Security REST
在 MyModuleGrailsPlugin.groovy 中添加以下行可确保模块以正确的顺序加载:
def loadAfter = [
'springSecurityRest'
]
在 grails 2 中,覆盖 XPlugin.groovy 中的 bean 只需要在 doWithSpring() 方法中重新定义服务:
def doWithSpring = {
tokenStorageService(TokenStorageProxyService) { it.autowire = 'byName' }
}
我开始在 Grails 3 中编程,已经有了 Grails 2 的经验,并在新插件中尝试了相同类型的覆盖,因为我正在为我现有的一些 Grails 2 插件创建 Grails 3 版本:
Closure doWithSpring() { {->
tokenStorageService(TokenStorageProxyService) {
it.autowire = 'byName'
}
} }
我做了一些集成测试:
@Integration
class SecurityServiceIntegrationSpec extends Specification {
SecurityService securityService
TokenStorageProxyService tokenStorageService
...
}
当 运行 测试时,我得到错误:
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'grails.plugin.springsecurity.rest.token.storage.jwt.JwtTokenStorageService' to required type 'com.b2boost.grails3.auth.client.TokenStorageProxyService' for property 'tokenStorageService'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'grails.plugin.springsecurity.rest.token.storage.jwt.JwtTokenStorageService' to required type 'com.b2boost.grails3.auth.client.TokenStorageProxyService' for property 'tokenStorageService': no matching editors or conversion strategy found at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:605) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:400) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ... at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalStateException: Cannot convert value of type 'grails.plugin.springsecurity.rest.token.storage.jwt.JwtTokenStorageService' to required type 'com.b2boost.grails3.auth.client.TokenStorageProxyService' for property 'tokenStorageService': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306) at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590) ... 37 more
默认的 JwtTokenStorageService 似乎没有被我的 TokenStorageProxyService 覆盖,导致在将服务连接到集成测试时出现 ConversionNotSupportedException。如何覆盖我的插件中的服务?
经过一些调查,我的自定义模块似乎是在 Spring 安全休息后加载的。因此,来自 SSR 的 tokenStorageService 覆盖了我的服务,而相反的情况应该发生:
Configuring MyModule...
... finished configuring MyModule
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Configuring Spring Security REST 2.0.0.M2...
... finished configuring Spring Security REST
在 MyModuleGrailsPlugin.groovy 中添加以下行可确保模块以正确的顺序加载:
def loadAfter = [
'springSecurityRest'
]