简体   繁体   English

使用Groovy命令行脚本提取Java Spring Beans如何停止NoSuchBeanDefinitionException?

[英]Using Groovy command line script to exectue Java Spring Beans how do I stop the NoSuchBeanDefinitionException?

I have written this Groovy script to load and execute some Java Spring Beans: 我已经编写了这个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");
        }
    }

The application context I am trying to load has this bean declaration: 我尝试加载的应用程序上下文具有以下bean声明:

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

The exception I get at command line is: 我在命令行中得到的异常是:

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)

However, running this script causes a NoSuchBeanDefinitionException on line 40 as if Spring cannot find the bean spec. 但是,运行此脚本会在第40行导致NoSuchBeanDefinitionException,好像Spring无法找到bean规范。

If I use a Spring partial integration test written in Java and running in Eclipse I can load the bean and the other beans it needs just fine. 如果我使用用Java编写并在Eclipse中运行的Spring部分集成测试,则可以加载bean和其他需要的bean。 It loads as expected. 它按预期加载。

I inspected the jars downloaded by Grape located at my Windows Home /.groovy and all jars are present. 我检查了位于Windows Home /.groovy上的Grape下载的jar,所有的jar都存在。

Based on my reading of Adding Things to the Classpath Grape should be taking care of getting these jars on my classpath. 根据我对“ 向类路径添加东西”的阅读,Grape应该注意将这些罐子放在类路径中。

I am running: 我在跑步:

  • Windows XP Windows XP
  • Eclipse Juno Eclipse朱诺
  • Groovy 2.0.6 Groovy 2.0.6
  • Sun JVM: 1.6.0_33 Sun JVM:1.6.0_33
  • Spring 3.0.5.RELEASE Spring 3.0.5。发布
  • Artifactory 2.3.4 人工工厂2.3.4

I have found out by working on a Grails project using the same services that I am able to get Groovy Grapes to download the dependencies and Spring to resolve them is I simply do this: 我通过使用与Groovy Grapes能够下载依赖项并使用Spring解决它们的相同服务进行的Grails项目工作发现,我只是这样做:

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

The key differences is I cleaned up the classpath pattern. 关键区别在于我清理了类路径模式。 The other patterns all failed both in my Grails app and here. 其他模式在我的Grails应用程序和此处均失败。

Give your bean a name attribute and pass that to getBean instead of the id . 给您的bean一个name属性,并将其传递给getBean而不是id

Edit: 编辑:

You should be seeing something like this being logged: 您应该看到这样的记录:

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@4ed1102f: defining beans [gapAnalyzer]; root of factory hierarchy

In particular, it should show a message with the file name and list the declared beans. 特别是,它应该显示带有文件名的消息并列出已声明的bean。 If you're not seeing this, the XML configuration file is not being read. 如果没有看到此消息,则说明未读取XML配置文件。 Spring fails silently if it can't find it. 如果找不到,Spring会静默失败。

This script: 该脚本:

@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'

With this XML file in the same directory works for me: 在同一个目录中使用此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>

I don't think the ClassPathXmlApplicationContext ever expands its constructor argument into a Resource[] array. 我认为ClassPathXmlApplicationContext从未将其构造函数参数扩展为Resource[]数组。 So Spring thinks you are trying to create a context from a non-existent (single) file called **/META-INF/applicationContext*.xml . 所以Spring认为您正在尝试从不存在的(单个)文件**/META-INF/applicationContext*.xml创建一个上下文。 Unsurprisingly that doesn't work (and wouldn't work in Java or Groovy). 毫不奇怪,这是行不通的(在Java或Groovy中也行不通)。 The ant-style patterns are only expanded when a String needs to be converted to a Resource[] , for instance when specifying an <import/> in XML. 仅当需要将String转换为Resource[]时(例如,在XML中指定<import/>Resource[] ,才扩展蚂蚁风格的模式。 If you want to do it without an import I suggest you use the ResourceArrayPropertyEditor directly in conjunction with a GenericXmlApplicationContext (which accepts a Resource[] as a constructor argument). 如果要在不导入的情况下执行此操作,建议您直接将ResourceArrayPropertyEditorGenericXmlApplicationContext (将Resource[]作为构造函数参数接受)结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM