/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.codehaus.groovy.grails.web.servlet;

import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.spring.SpringConfig;
import org.springframework.beans.BeansException;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springmodules.beans.factory.drivers.xml.XmlWebApplicationContextDriver;

/**
 * <p>Servlet that handles incoming requests for Grails.
 * 
 * <p>This servlet loads the Spring configuration based on the Grails application
 * in the parent application context. 
 * 
 * @author Steven Devijver
 * @since Jul 2, 2005
 */
public class GrailsDispatcherServlet extends DispatcherServlet {

	private GrailsApplication application = null;
	
	public GrailsDispatcherServlet() {
		super();
	}

	public void setApplication(GrailsApplication application) {
		this.application = application;
	}
	
	protected WebApplicationContext createWebApplicationContext(
			WebApplicationContext parent) throws BeansException {
		SpringConfig springConfig = new SpringConfig(application);
		return new XmlWebApplicationContextDriver().getWebApplicationContext(springConfig.getBeanReferences(), parent, getServletContext(), getNamespace(), StringUtils.tokenizeToStringArray(
				getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
	}
}

@@ -1481,35 +1481,68 @@
 private 
-GrailsApplication a
+static final String GRAILS_APPLICATION_ID = %22grailsA
 pplicati
@@ -1547,18 +1547,13 @@
 tion
- = null
+%22
 ;%0A%09
+%09
 %0A%09pu
@@ -1604,108 +1604,8 @@
 %09%7D%0A%0A
-%09public void setApplication(GrailsApplication application) %7B%0A%09%09this.application = application;%0A%09%7D%0A%09%0A
 %09pro
@@ -1719,16 +1719,133 @@
 ption %7B%0A
+%09%09GrailsApplication application = (GrailsApplication)parent.getBean(GRAILS_APPLICATION_ID, GrailsApplication.class);%0A
 %09%09Spring
@@ -1897,16 +1897,61 @@
 ation);%0A
+%09%09if (getContextConfigLocation() != null) %7B%0A%09
 %09%09return
@@ -2224,12 +2224,187 @@
 ERS));%0A%09
-%7D
+%09%7D else %7B%0A%09%09%09return new XmlWebApplicationContextDriver().getWebApplicationContext(springConfig.getBeanReferences(), parent, getServletContext(), getNamespace(), null);%0A%09%09%7D%0A%09%7D%0A%09
 %0A%7D%0A

Fix way Grails application instance is retrieved from application context.