2012-12-17 86 views
-1

我有一個問題,試圖擴展一個groovy類。我已經擴展了一個groovy類,它通過構造文件路徑從Spring應用程序上下文文件加載,使用Spring的BeanDefinitionRegistry註冊bean,然後從應用程序上下文中檢索它。擴展groovy類導致運行時異常當檢索bean

private BeanDefinitionRegistry registry; 

...

GenericBeanDefinition bd = new GenericBeanDefinition(); 
    bd.setBeanClass(GroovyScriptFactory.class); 
    bd.setAttribute(
      ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 
      refreshDelay); 

    ConstructorArgumentValues cav = bd.getConstructorArgumentValues(); 
    cav.addGenericArgumentValue(groovyLocation + WordUtils.capitalize(name) 
      + ".groovy"); 

    registry.registerBeanDefinition(name, bd); 

...

 // retrieve it 
     ExternalObject engine = (ExternalObject) this.applicationContext 
       .getBean(engineName); 

這似乎除了當我向Groovy類,像這樣的工作好嗎:

Cat.groovy :

package com.mycomp.external.engine; 

import com.mycomp.external.ExternalObject; 

    public class Cat extends ExternalObject { 

    } 

Lion.groovy

package com.mycomp.external.engine; 

    public class Lion extends Cat { 

    } 

然後我得到一個錯誤的這種完全爛攤子:

Lion: 4: unable to resolve class Cat 
@ line 4, column 1. 
    public class Lion extends Cat { 
^

1 error 

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at com.mycomp.external.ExternalAdFactoryImpl.getEngineByName(ExternalAdFactoryImpl.java:119) [adverter-1.9.0-SNAPSHOT.jar:na] 
    at com.mycomp.external.BasicEngineTest.getEngine(BasicEngineTest.java:62) [test-classes/:na] 
    at com.mycomp.external.BasicEngineTest.realModeGet(BasicEngineTest.java:108) [test-classes/:na] 
    at com.mycomp.external.BasicEngineTest.testMany(BasicEngineTest.java:123) [test-classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_37] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [na:1.6.0_37] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [na:1.6.0_37] 
    at java.lang.reflect.Method.invoke(Method.java:597) [na:1.6.0_37] 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:76) [testng-6.0.1.jar:na] 
    at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:178) [testng-6.0.1.jar:na] 
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:158) [spring-test-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_37] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [na:1.6.0_37] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [na:1.6.0_37] 
    at java.lang.reflect.Method.invoke(Method.java:597) [na:1.6.0_37] 
    at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:191) [testng-6.0.1.jar:na] 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:666) [testng-6.0.1.jar:na] 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846) [testng-6.0.1.jar:na] 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170) [testng-6.0.1.jar:na] 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) [testng-6.0.1.jar:na] 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) [testng-6.0.1.jar:na] 
    at org.testng.TestRunner.runWorkers(TestRunner.java:1147) [testng-6.0.1.jar:na] 
    at org.testng.TestRunner.privateRun(TestRunner.java:749) [testng-6.0.1.jar:na] 
    at org.testng.TestRunner.run(TestRunner.java:600) [testng-6.0.1.jar:na] 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:317) [testng-6.0.1.jar:na] 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312) [testng-6.0.1.jar:na] 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274) [testng-6.0.1.jar:na] 
    at org.testng.SuiteRunner.run(SuiteRunner.java:223) [testng-6.0.1.jar:na] 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) [testng-6.0.1.jar:na] 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) [testng-6.0.1.jar:na] 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039) [testng-6.0.1.jar:na] 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:964) [testng-6.0.1.jar:na] 
    at org.testng.TestNG.run(TestNG.java:900) [testng-6.0.1.jar:na] 
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:70) [surefire-testng-2.8.1.jar:2.8.1] 
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:102) [surefire-testng-2.8.1.jar:2.8.1] 
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:114) [surefire-testng-2.8.1.jar:2.8.1] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_37] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [na:1.6.0_37] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [na:1.6.0_37] 
    at java.lang.reflect.Method.invoke(Method.java:597) [na:1.6.0_37] 
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103) [surefire-booter-2.8.1.jar:2.8.1] 
    at $Proxy0.invoke(Unknown Source) [na:na] 
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150) [surefire-booter-2.8.1.jar:2.8.1] 
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91) [surefire-booter-2.8.1.jar:2.8.1] 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) [surefire-booter-2.8.1.jar:2.8.1] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Lion': Could not determine scripted object type for GroovyScriptFactory: script source locator [file:///Users/jkramer/source/backend/backend/config/../feeds/src/main/groovy/com/mycomp/external/engine/Lion.groovy]; nested exception is org.springframework.scripting.ScriptCompilationException: Could not compile script; nested exception is org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Lion: 4: unable to resolve class Cat 
@ line 4, column 1. 
    public class Lion extends Cat { 
^

1 error 

    at org.springframework.scripting.support.ScriptFactoryPostProcessor.postProcessBeforeInstantiation(ScriptFactoryPostProcessor.java:297) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:848) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:820) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:446) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    ... 50 common frames omitted 
Caused by: org.springframework.scripting.ScriptCompilationException: Could not compile script; nested exception is org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Lion: 4: unable to resolve class Cat 
@ line 4, column 1. 
    public class Lion extends Cat { 
^

1 error 

    at org.springframework.scripting.groovy.GroovyScriptFactory.getScriptedObjectType(GroovyScriptFactory.java:217) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    at org.springframework.scripting.support.ScriptFactoryPostProcessor.postProcessBeforeInstantiation(ScriptFactoryPostProcessor.java:290) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    ... 53 common frames omitted 
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Lion: 4: unable to resolve class Cat 
@ line 4, column 1. 
    public class Lion extends Cat { 
^

1 error 

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302) [groovy-all-1.8.0.jar:1.8.0] 
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:854) [groovy-all-1.8.0.jar:1.8.0] 
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544) [groovy-all-1.8.0.jar:1.8.0] 
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:493) [groovy-all-1.8.0.jar:1.8.0] 
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306) [groovy-all-1.8.0.jar:1.8.0] 
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287) [groovy-all-1.8.0.jar:1.8.0] 
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267) [groovy-all-1.8.0.jar:1.8.0] 
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:214) [groovy-all-1.8.0.jar:1.8.0] 
    at org.springframework.scripting.groovy.GroovyScriptFactory.getScriptedObjectType(GroovyScriptFactory.java:201) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] 
    ... 54 common frames omitted 
17:26:54.942 [Hector.me.prettyprint.cassandra.connection.CassandraHostRetryService-1] INFO m.p.c.c.CassandraHostRetryService - Not checking that 10.10.0.111(10.10.0.111):9160 is a member of the ring since there are no live hosts 

任何幫助,將不勝感激!

+0

對不起,我想我不太瞭解這個社區是如何運作的。我會更好參與:-) – Kramer

回答

0

似乎沒有直接的方式將兩者都包含在源路徑中(除非您在cav.addGenericArgumentValue(groovyLocation + WordUtils.capitalize(name) + ".groovy")中設置的定位器允許在與classpath:樣式定位器路徑一起使用時使用多個類,這一點我很懷疑)。我認爲它的目的是讓你的腳本能夠自給自足。

您可以嘗試將兩者合併爲一個.groovy類,但「24.3.3。Groovy beans」here的結尾似乎將kibosh放在該位置。

如果您可以使用與加載ExternalObject類相同的機制加載超類,我認爲這可能會這樣做(儘管這不太可能是您要找的)。

如果您可以避免使用繼承,而是在您的Lion類中使用封裝,那麼您可能會在加載的類中使用groovy類加載器來修飾某些內容以吸引其他類,但我承認它只是一種在那裏揮手。

相關問題