我正在使用Grails 2.3.11。 在BootStrap.groovy中,我打电话给:
def ctx = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
HotSwappableTargetSource ts = ctx.getBean("urlMappingsTargetSource")
我收到NoSuchBeanDefinitionException
12,09 21:55:44:084 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: No bean named 'urlMappingsTargetSource' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'urlMappingsTargetSource' is defined
at BootStrap$_closure1.doCall(BootStrap.groovy:10)
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308)
at grails.util.Environment.executeForEnvironment(Environment.java:301)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
当我在开发环境中运行应用程序时,这不会引发异常。但是,如果我构建一个war文件并部署到tomcat,我会得到一个org.springframework.beans.factory.NoSuchBeanDefinitionException
使用Grails 2.1.0和2.2.4时,此行正常工作。我也尝试过Grails 2.3.9,但这也会抛出NoSuchBeanDefinitionException。
我也尝试使用依赖注入,但“urlMappingsTargetSource”为空
class BootStrap {
def urlMappingsTargetSource
是否有其他人遇到此问题,或者有人知道我为什么会收到NoSuchBeanDefinitionException吗?
这个bean在Grails 2.3和Grails 2.2或2.1中的加载方式有什么变化吗?
非常感谢任何帮助或见解。
答案 0 :(得分:3)
UrlMappingsGrailsPlugin.groovy包含以下代码
if (Environment.isDevelopmentMode() || Environment.current.isReloadEnabled()) {
"org.grails.internal.URL_MAPPINGS_HOLDER"(UrlMappingsHolderFactoryBean) { bean ->
bean.lazyInit = true
}
urlMappingsTargetSource(HotSwappableTargetSource, ref("org.grails.internal.URL_MAPPINGS_HOLDER")) { bean ->
bean.lazyInit = true
}
grailsUrlMappingsHolder(ProxyFactoryBean) { bean ->
bean.lazyInit = true
targetSource = urlMappingsTargetSource
proxyInterfaces = [UrlMappings]
}
} else {
grailsUrlMappingsHolder(UrlMappingsHolderFactoryBean) { bean ->
bean.lazyInit = true
}
}
如果envrionment不是开发模式或启用重新加载,则bean不会被初始化。 我将bean添加到我的resources.groovy并且它有效
import org.codehaus.groovy.grails.web.mapping.UrlMappingsHolderFactoryBean
import org.springframework.aop.framework.ProxyFactoryBean
import org.springframework.aop.target.HotSwappableTargetSource
// Place your Spring DSL code here
beans = {
"org.grails.internal.URL_MAPPINGS_HOLDER"(UrlMappingsHolderFactoryBean) { bean ->
bean.lazyInit = true
}
urlMappingsTargetSource(HotSwappableTargetSource, ref("org.grails.internal.URL_MAPPINGS_HOLDER")) { bean ->
bean.lazyInit = true
}
grailsUrlMappingsHolder(ProxyFactoryBean) { bean ->
bean.lazyInit = true
targetSource = urlMappingsTargetSource
proxyInterfaces = [org.codehaus.groovy.grails.web.mapping.UrlMappings]
}
}