【问题标题】:How to prevent Grails from trying to use a domain class with Service in name as a service?如何防止 Grails 尝试使用名称为 Service 的域类作为服务?
【发布时间】:2014-06-04 15:12:29
【问题描述】:

我的应用程序中有一个名为 ContractedService 的域类。但是,grails 似乎将其识别为服务类(即使它位于 grails-app/domain 目录中)。如果不完全重命名类,如何防止这种情况发生?

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 将其视为服务,每个打开它的开发人员都希望看到的是服务,而不是域类。起名好难! :)
  • 不仅如此,Grails 还基于约定俗成的编码思想。当你打破约定时,你就打破了 Grails。 (可以这么说)。我认为您应该重命名 Domain 类。
  • 很公平,我可能会继续前进,但我仍然有兴趣知道答案。使服务发现依赖于路径似乎很容易,因为域对象和服务应该位于不同的路径上。

标签: grails groovy


【解决方案1】:

您应该坚持 grails 约定。因此,您可以将您的域类重命名为 ContractedServiceDefinition 并使用原始域名表映射:

static mapping = { table:'contracted_service'}

【讨论】:

  • 当然,但据我了解,惯例是将服务放在 grails-app/services 目录中。我认为只有以 Service 结尾并放置在该目录中的类才应该被识别为服务,这似乎与文档一致。在这种情况下,域类被识别为服务。对我来说似乎是一个错误。
猜你喜欢
  • 2017-12-30
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
相关资源
最近更新 更多