我寫了這個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
我試過這種方法,但我仍然遇到同樣的問題。我添加了一個名稱屬性,名稱與id不同(因爲我們的標準/管理需要id,所以我留下了id)。我甚至嘗試將有問題的瓶子移動到我常規安裝的lib文件夾中,並將它們從〜/ .groovy中移除,併爲我們的bean罐註釋掉葡萄抓鬥。將嘗試刪除id屬性並保留名稱屬性。 – fergjo
對不起,我認爲那是一隻紅鯡魚。這個問題可能與查找彈簧配置有關。見上面的編輯。 – ataylor
好吧,即使Groovy可以(即在我的腳本中有導入和引用工作),Spring似乎也找不到它。關於加載上下文,沒有看到Spring提供的INFO: 'INFO:在org.s[email protected]21b42f中預先實例化單例:定義beans [];根據工廠層次結構' – fergjo