如何防止Grails尝试将名为Service的域类作为服务使用?
我的应用程序中有一个名为ContractedService的域类。然而,grails似乎将其识别为一个服务类(即使它位于grails app/domain目录中)。在不完全重命名类的情况下,如何防止这种情况如何防止Grails尝试将名为Service的域类作为服务使用?,grails,groovy,Grails,Groovy,我的应用程序中有一个名为ContractedService的域类。然而,grails似乎将其识别为一个服务类(即使它位于grails app/domain目录中)。在不完全重命名类的情况下,如何防止这种情况 org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Cannot add Service class [class com.myapp.ContractedService]. It is not a Serv
org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Cannot add Service class [class com.myapp.ContractedService]. It is not a Service!
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:750)
at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:474)
at grails.test.mixin.services.ServiceUnitTestMixin.mockService(ServiceUnitTestMixin.groovy:46)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.MethodInvocation.invokeTargetMethod(MethodInvocation.java:91)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:85)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$1$2.evaluate(TestRuntimeJunitAdapter.groovy:48)
at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:138)
at org.spockframework.runtime.extension.MethodInvocation.invokeTargetMethod(MethodInvocation.java:91)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:85)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at grails.test.runtime.TestRuntimeJunitAdapter$3$4.evaluate(TestRuntimeJunitAdapter.groovy:74)
at org.spockframework.runtime.extension.builtin.ClassRuleInterceptor.intercept(ClassRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
您应该坚持grails约定。因此,您可以将域类重命名为ContractedServiceDefinition,并使用原始域名表映射:
static mapping = { table:'contracted_service'}
这听起来可能没有太大帮助,但您确实应该重命名该类。即使您可以阻止Grails将其视为服务,打开它的每个开发人员都会期望看到服务,而不是域类。命名很难!:)不仅如此,Grails还基于按约定编码的思想。当你打破惯例,你就打破了圣杯。(可以这么说)。我认为你应该重新命名域类。这很公平,我可能会继续前进,但我仍然有兴趣知道答案。让服务发现依赖于路径似乎是一件容易的事情,因为域对象和服务应该位于不同的路径上。我认为只有以服务结尾并放在该目录中的类才应该被识别为服务,这似乎与文档一致。在这种情况下,域类被识别为服务。对我来说好像是个虫子。