0

いくつかのJavaSpringBeanをロードして実行するために、このGroovyスクリプトを作成しました。

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

ただし、このスクリプトを実行すると、SpringがBeanの仕様を見つけられないかのように、40行目にNoSuchBeanDefinitionExceptionが発生します。

Javaで記述され、Eclipseで実行されているSpring部分統合テストを使用すると、Beanとその他の必要なBeanを正常にロードできます。期待どおりにロードされます。

Windows Home /.groovyにあるGrapeによってダウンロードされたjarを調べたところ、すべてのjarが存在しています。

クラスパスに物を追加するという私の読書に基づいて、グレープはこれらのjarファイルをクラスパスに配置するように注意する必要があります。

私は走っています:

  • Windows XP
  • Eclipse Juno
  • Groovy 2.0.6
  • Sun JVM:1.6.0_33
  • 春3.0.5。リリース
  • アーティファクトリー2.3.4
4

4 に答える 4

1

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:

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.

于 2013-04-25T15:09:38.000 に答える
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>
于 2013-02-12T01:49:11.707 に答える
0

Beanにname属性を指定し、getBeanの代わりにそれを渡しますid

編集:

次のようなログが記録されているはずです。

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

特に、ファイル名を含むメッセージを表示し、宣言されたBeanをリストする必要があります。これが表示されない場合は、XML構成ファイルが読み取られていません。Springが見つからない場合、Springは黙って失敗します。

于 2013-01-21T19:25:17.730 に答える
0

I don't think the ClassPathXmlApplicationContext ever expands its constructor argument into a Resource[] array. So Spring thinks you are trying to create a context from a non-existent (single) file called **/META-INF/applicationContext*.xml. Unsurprisingly that doesn't work (and wouldn't work in Java or 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. 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).

于 2013-02-13T12:32:49.470 に答える