我编写了这个 Groovy 脚本来加载和执行一些 Java Spring Bean:
@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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@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 和它需要的其他 bean。它按预期加载。
我检查了位于我的 Windows 主页/.groovy的 Grape 下载的罐子,并且所有罐子都存在。
根据我对向类路径添加东西的阅读,葡萄应该注意在我的类路径中获取这些罐子。
我在跑步:
- 视窗
- 日蚀朱诺
- Groovy 2.0.6
- 太阳 JVM:1.6.0_33
- Spring 3.0.5.RELEASE
- 神器2.3.4