从Grails脚本加载bean

时间:2012-03-13 17:30:59

标签: grails dependencies

我正在运行grails脚本来从grails应用程序加载bean,但是,似乎我有一个依赖性问题。这是我的代码:

import grails.spring.BeanBuilder
import org.springframework.context.ApplicationContext

target(main: "Script to load location information into Solr") {

println "Hello script"
def bb = new BeanBuilder()
ApplicationContext appContext = bb.createApplicationContext()
def service = appContext.getBean("solrjService")   
}

setDefaultTarget(main)

当我执行脚本时,我得到以下stacktrace:

main:
Hello script
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.staticInitialize(LoggerFactory.java:83)
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:73)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:131)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:272)
at grails.spring.BeanBuilder.<clinit>(BeanBuilder.java:84)

任何想法??

感谢您的时间

2 个答案:

答案 0 :(得分:2)

如果你读了调用堆栈,那显然是SLF4J的一个问题。

请参阅http://slf4j.org/faq.html#IllegalAccessError

看起来你可能会混合使用SLF4J罐子的版本,并且会发生冲突。

但是,当然,Burt是正确的 - 一旦你超越了这个,你会发现你已经初始化了BeanBuilder的ApplicationContext而没有bean。

答案 1 :(得分:1)

我必须包含目标_GrailsBootstrap才能加载我的bean http://grails.org/doc/latest/guide/commandLine.html#creatingGantScripts

includeTargets << grailsScript("_GrailsBootstrap")

target ('default': "Load Location Information to Solr Server") {

depends(configureProxy, packageApp, classpath, loadApp, configureApp)
def service = appCtx.getBean('solrjService')
println service.getLocationSuggestion("Barcelona")

}

我按照他的方式运行脚本(这就是我遇到类路径问题的原因)

grails run-script scripts/Myscript.groovy

然后现在,我这样运行

grails Myscript.groovy

我没有任何类路径问题:D

感谢您的帮助