Grails:如何引用 Service 本身并在 Java 中使用它?

标签 grails service reference

A SomeService 被定义并在一个闭包中传递给一个 Java 类:

package somepackage

class SomeService{
    ...
    def process(){
        ...
        //here, how to reference [thisService]? this keyword?
        //I tried this keyword, but an exception occurs
        JavaClass javaObject = new JavaClass(thisService, otherParams)
        ...
    }
    ...
}

使用 时抛出异常此 SomeService :
Groovyc: java.lang.NoClassDefFoundError: somepackage/SomeService
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethods(Class.java:1791)
at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(Java5.java:318)
at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:263)
at org.codehaus.groovy.ast.ClassNode.getUnresolvedSuperClass(ClassNode.java:957)
at org.codehaus.groovy.ast.ClassNode.getUnresolvedSuperClass(ClassNode.java:952)
at org.codehaus.groovy.ast.ClassNode.getSuperClass(ClassNode.java:946)
at org.codehaus.groovy.ast.ClassNode.isDerivedFrom(ClassNode.java:889)
at org.codehaus.groovy.classgen.AsmClassGenerator.assignmentCastAndVisit(AsmClassGenerator.java:4049)
at org.codehaus.groovy.classgen.AsmClassGenerator.evaluateEqual(AsmClassGenerator.java:4002)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitDeclarationExpression(AsmClassGenerator.java:1468)
at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:53)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4111)
at org.codehaus.groovy.classgen.AsmClassGenerator.evaluateExpression(AsmClassGenerator.java:1437)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:1398)
at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:47)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:165)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:728)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:616)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:591)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:686)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1039)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:276)
at org.codehaus.groovy.control.CompilationUnit$12.call(CompilationUnit.java:748)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:942)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:519)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:497)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:474)
at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:54)
at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:80)
at org.jetbrains.groovy.compiler.rt.GroovycRunner.main(GroovycRunner.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:121)
Caused by: java.lang.ClassNotFoundException: entrust.EnTrustService
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 43 more

我的问题是:如何将服务本身引用到其他类?如果 是否适用,这个问题是否与调用类是 groovy 类以外的 Java 类有关?

最佳答案

您可以引用 someService bean 来自 ApplicationContext作为:

class SomeService{
    def grailsApplication

    def process(){
        ...
        //here, how to reference [thisService]? this keyword?
        //I tried this keyword, but an exception occurs
        JavaClass javaObject = 
            new JavaClass(grailsApplication.mainContext.someService, otherParams)
        ...
    }
}

这样做时,控制会离开代理并从上下文中获取 bean。同样的原则适用于事务或任何方面 (AOP) 相关的从一种方法到同一服务类中的另一种方法的转换。这是一个 example .

关于Grails:如何引用 Service 本身并在 Java 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20754038/

相关文章:

Grails Spring Security 插件 - 登录表单重定向到 ajaxAuth

C++ 通过引用返回

android - 无法启动服务 Intent

android - 位置监听器从服务工作,但不是 IntentService

android - 远程激活 android 服务 - 即使在锁定的设备上

c++ - 如何将指针传递给构造函数?

c# - 如何在不更改引用的情况下将两个字符串添加在一起

xml - Grails 错误 : invalid XML character (Unicode: 0x5c)

unit-testing - 在 Controller 中使用g.applyLayout破坏单元测试

grails - 在 Grails 中用于测试的不同 resources.xml?