Grails 3:在doWithSpring中覆盖服务

时间:2017-03-21 11:44:20

标签: grails integration-testing grails3 grails3.2.0

在grails 2中,覆盖XPlugin.groovy中的bean,只需要在doWithSpring()方法中重新定义服务:

def doWithSpring = {
    tokenStorageService(TokenStorageProxyService) { it.autowire = 'byName' }
}

我开始使用Grails 3进行编程,已经有过使用Grails 2的经验并尝试在新插件中使用相同类型的覆盖,因为我正在创建一些现有Grails 2插件的Grails 3版本:< / p>

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

似乎我的TokenStorageProxyService没有覆盖默认的JwtTokenStorageService,当服务连接到集成测试时导致ConversionNotSupportedException。如何覆盖插件中的服务?

1 个答案:

答案 0 :(得分:0)

经过一番调查,似乎我的自定义模块是在Spring安全休息后加载的。 SSR中的tokenStorageService因此覆盖了我的服务,而反过来应该发生:

  

配置MyModule ...

     

...完成配置MyModule

     

配置Spring Security Core ...

     

...完成配置Spring Security Core

     

配置Spring Security REST 2.0.0.M2 ...

     

...完成配置Spring Security REST

在MyModuleGrailsPlugin.groovy中添加以下行确保以正确的顺序加载模块:

def loadAfter = [
    'springSecurityRest'
]