2013-01-21 158 views
0

我寫了這個Groovy腳本加載並執行一些Java的Spring Bean:使用Groovy命令行腳本來執行Java Spring Bean如何停止NoSuchBeanDefinitionException?

@GrabResolver(name = 'libs.snapshot', root = 'http://ml1002pc:8081/artifactory/libs-snapshot', m2compatible = 'true') 
    @Grapes([ 
    @Grab(group = 'com.siemens.soarian.sf', module = 'BuildInformationService', version = '1.0-SNAPSHOT', changing = true), 
    @Grab(group = 'com.siemens.soarian.sf', module = 'GapAnalyzer', version = '1.0-SNAPSHOT', changing = true) 
    ]) 

    @GrabResolver(name = 'libs-release', root = 'http://ml1002pc:8081/artifactory/libs-release', m2compatible = 'true') 
    @Grapes([ 
    @Grab(group = 'org.springframework', module = 'org.springframework.context', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.context.support', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.core', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.beans', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.asm', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.aop', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.aspects', version = '3.0.5.RELEASE'), 
    @Grab(group = 'org.springframework', module = 'org.springframework.expression', version = '3.0.5.RELEASE'), 
    @Grab('org.apache.commons:commons-lang:2.6'), 
    @Grab('org.apache.commons:commons-collections:3.2.1'), 
    @Grab('org.apache.commons:commons-logging:1.1.1'), 
    @Grab('org.apache.commons:commons-dbcp:1.4'), 
    @Grab('org.apache.commons:commons-pool:1.6'), 
    @Grab('com.microsoft:sqljdbc:1.2'), 
    ]) 
    @GrabConfig(systemClassLoader = true) 

    import java.util.Collection; 
    import java.util.List; 

    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 

    import com.siemens.soarian.sf.build.BuildInformationService; 
    import com.siemens.soarian.sf.gap.* 

    public class CommandLineWrapper { 

     public static void main(String[] args) { 
      ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:**/META-INF/applicationContext*.xml"); 

      GapAnalyzer gapAnalyzer = (GapAnalyzer) ctx.getBean("gapAnalyzer"); 
     } 
    } 

我試圖加載應用程序上下文有這個bean聲明:

<bean id="gapAnalyzer" class="com.siemens.soarian.sf.gap.GapAnalyzerImpl"> 
    <constructor-arg ref="buildInformationService"/> 
    </bean> 

的例外,我在命令得到行是:

INFO: Refreshing org[email protected]14f1726: startup date [Mon Jan 21 10:46:04 EST 2013 ]; root of context hierarchy 
Jan 21, 2013 10:46:04 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]1e22c75: defining beans []; root of factory hierarchy 
Caught: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'gapAnalyzer' is defined 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'gapAnalyzer' is defined 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075) 
     at org.springframework.beans.factory.BeanFactory$getBean.call(Unknown Source) 

但是,運行此腳本會導致第40行NoSuchBeanDefinitionException,就好像Spring無法找到bean規範。

如果我使用用Java編寫並在Eclipse中運行的Spring部分集成測試,我可以加載它和其他需要的bean。它按預期加載。

我檢查了位於我的Windows Home的Grape下載的罐子,並且所有的罐子都存在於我的Windows Home /.groovy中。

根據我對Adding Things to the Classpath的閱讀,葡萄應該照顧在我的班級路徑上獲得這些罐子。

我運行:

  • 的Windows XP
  • 的Eclipse朱諾
  • Groovy的2.0.6
  • 了Sun JVM:1.6.0_33
  • 春3.0.5.RELEASE
  • Artifactory的2.3.4

回答

1

我以我能夠得到的Groovy葡萄下載的依賴和Spring來解決這些問題是我根本就這個相同的服務上一個Grails項目的工作發現差異是我清理了類路徑模式。其他模式都在我的Grails應用程序和這裏都失敗了。

0

給你的豆一個name屬性,並將其傳遞給getBean而不是id

編輯:

你應該看到像這樣被記錄:

INFO: Refreshing org[email protected]318a41cf: startup date [Mon Jan 21 20:51:21 MST 2013]; root of context hierarchy 
INFO: Loading XML bean definitions from file [./META-INF/spring-conf.xml] 
INFO: Pre-instantiating singletons in org.s[email protected]4ed1102f: defining beans [gapAnalyzer]; root of factory hierarchy 

特別是,它應該顯示的文件名的消息,並列出聲明豆。如果你沒有看到這個,XML配置文件沒有被讀取。如果無法找到它,Spring將自動失敗。

+0

我試過這種方法,但我仍然遇到同樣的問題。我添加了一個名稱屬性,名稱與id不同(因爲我們的標準/管理需要id,所以我留下了id)。我甚至嘗試將有問題的瓶子移動到我常規安裝的lib文件夾中,並將它們從〜/ .groovy中移除,併爲我們的bean罐註釋掉葡萄抓鬥。將嘗試刪除id屬性並保留名稱屬性。 – fergjo

+0

對不起,我認爲那是一隻紅鯡魚。這個問題可能與查找彈簧配置有關。見上面的編輯。 – ataylor

+0

好吧,即使Groovy可以(即在我的腳本中有導入和引用工作),Spring似乎也找不到它。關於加載上下文,沒有看到Spring提供的INFO: 'INFO:在org.s[email protected]21b42f中預先實例化單例:定義beans [];根據工廠層次結構' – fergjo

0

這個腳本:

@Grab('org.springframework:spring-context:3.2.1.RELEASE') 
@Grab('org.apache.commons:commons-lang3:3.1') 
@GrabConfig(systemClassLoader = true) 
import org.springframework.context.support.ClassPathXmlApplicationContext 

def ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml") 
def su = ctx.getBean('su') 
assert su.swapCase('Fee Fi Fo Fum') == 'fEE fI fO fUM' 

在同一目錄下這個XML文件爲我工作:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
    <bean id="su" class="org.apache.commons.lang3.StringUtils"/> 
</beans> 
0

我不認爲ClassPathXmlApplicationContext不斷擴大其構造函數的參數爲​​Resource[]陣列。所以Spring認爲你試圖從一個名爲**/META-INF/applicationContext*.xml的不存在的(單個)文件創建一個上下文。毫不奇怪,這不起作用(並且在Java或Groovy中不起作用)。當需要將字符串轉換爲Resource[]時,例如在XML中指定<import/>時,纔會擴展ant樣式模式。如果你想不做導入,我建議你直接使用ResourceArrayPropertyEditorGenericXmlApplicationContext(它接受Resource[]作爲構造函數的參數)。

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:META-INF/applicationContext.xml"); 

的關鍵: