gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:30:11 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com