Skip to content
Browse files

fix for GRAILS-7851 "Grails 2.0.0M1 Domain Class named Environment"

  • Loading branch information...
1 parent ffc7b16 commit 021c06953ed472f9506b0b044e614ef347f7efa9 @graemerocher graemerocher committed Jan 25, 2012
View
11 ...g/src/main/groovy/org/codehaus/groovy/grails/commons/spring/GrailsApplicationContext.java
@@ -23,6 +23,7 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
@@ -43,6 +44,7 @@
protected MetaClass metaClass;
private BeanWrapper ctxBean = new BeanWrapperImpl(this);
private ThemeSource themeSource;
+ private static final String GRAILS_ENVIRONMENT_BEAN_NAME = "springEnvironment";
public GrailsApplicationContext(DefaultListableBeanFactory defaultListableBeanFactory) {
super(defaultListableBeanFactory);
@@ -157,4 +159,13 @@ public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues
bd.setPropertyValues(pvs);
getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
+
+ @Override
+ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
+ super.prepareBeanFactory(beanFactory);
+
+ // workaround for GRAILS-7851, until Spring allows the environment bean name to be configurable
+ ((DefaultListableBeanFactory)beanFactory).destroySingleton(ENVIRONMENT_BEAN_NAME);
+ beanFactory.registerSingleton(GRAILS_ENVIRONMENT_BEAN_NAME,getEnvironment());
+ }
}
View
49 ...st/groovy/org/codehaus/groovy/grails/orm/hibernate/DomainClassNamedEnvironmentSpec.groovy
@@ -0,0 +1,49 @@
+package org.codehaus.groovy.grails.orm.hibernate
+
+import grails.persistence.Entity
+import org.codehaus.groovy.grails.commons.GrailsApplication
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: graemerocher
+ * Date: 1/25/12
+ * Time: 2:33 PM
+ * To change this template use File | Settings | File Templates.
+ */
+class DomainClassNamedEnvironmentSpec extends GormSpec{
+ @Override
+ List getDomainClasses() {
+ [Environment]
+ }
+
+ void "Test that a domain class named environment doesn't conflict with Spring's environment bean"() {
+ when:"A service is called that injects the environment"
+ def environmentService = applicationContext.getBean(EnvironmentService)
+
+ then:"The service is usable"
+ environmentService.listEnvironments().size() == 0
+ }
+
+ @Override
+ protected void initializeApplication() {
+ grailsApplication.initialise()
+ domainClasses?.each { dc -> grailsApplication.addArtefact 'Domain', dc }
+ grailsApplication.addArtefact("Service", EnvironmentService)
+ grailsApplication.setApplicationContext(applicationContext)
+ parentCtx.registerMockBean(GrailsApplication.APPLICATION_ID, grailsApplication)
+
+ }
+
+
+}
+@Entity
+class Environment {
+ String name
+}
+
+class EnvironmentService {
+ Environment environment
+ List listEnvironments() {
+ Environment.list()
+ }
+}

0 comments on commit 021c069

Please sign in to comment.
Something went wrong with that request. Please try again.