Skip to content

Commit 0921e64

Browse files
committed
Revert "GRAILS-6790 - register the auto proxy creator in the “root” application context so that any configuration made by plugins/apps apply to our proxy creator."
Installing the APC in this way makes it not be used when generating transactional proxies, which means that Groovy classes don't automatically get proxied by class. Unfortunately, installing it in a way that does allow it to be used when generating transactional proxies runs up against the Spring bug SPR-7808 (which prevents usage of custom APC with 'aop' or 'tx' namespaces). This exercise has revealed that any user or plugin code that looks up the APC via it's well defined name during application bootstrap will not receive our custom instance. When Spring is doing this internally it will create an instance based on the default implementation. Once the Spring bug is fixed, the solution for us will be to replace line 72 of this patch we are reverting to: getSpringConfig().getUnrefreshedApplicationContext().beanFactory.registerBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME, proxyCreatorDefinition)
1 parent e311472 commit 0921e64

File tree

4 files changed

+15
-83
lines changed

4 files changed

+15
-83
lines changed

src/java/org/codehaus/groovy/grails/plugins/CoreGrailsPlugin.groovy

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ import org.codehaus.groovy.grails.support.proxy.DefaultProxyHandler
3535
import org.springframework.beans.factory.config.CustomEditorConfigurer
3636
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
3737
import org.springframework.beans.factory.xml.XmlBeanFactory
38-
import org.springframework.aop.config.AopConfigUtils
3938
import org.springframework.core.io.Resource
40-
import org.springframework.beans.factory.support.GenericBeanDefinition
4139
import org.codehaus.groovy.grails.aop.framework.autoproxy.GroovyAwareAspectJAwareAdvisorAutoProxyCreator
4240

4341
/**
@@ -58,18 +56,15 @@ class CoreGrailsPlugin {
5856
addBeanFactoryPostProcessor(new MapBasedSmartPropertyOverrideConfigurer(application))
5957
addBeanFactoryPostProcessor(new GrailsPlaceholderConfigurer())
6058

59+
// replace AutoProxy advisor with Groovy aware one
6160
def grailsConfig = application.config.grails
6261
def springConfig = grailsConfig.spring
63-
64-
// configure a Groovy aware AutoProxy advisor
65-
// we need to do this on the parent context to allow
66-
// plugins/apps to affect it while creating/configing beans
67-
// See GRAILS-6790
68-
def aspectJDisabled = springConfig.disable.aspectj.autoweaving
69-
def proxyCreatorType = aspectJDisabled ? GroovyAwareInfrastructureAdvisorAutoProxyCreator : GroovyAwareAspectJAwareAdvisorAutoProxyCreator
70-
def proxyCreatorDefinition = new GenericBeanDefinition()
71-
proxyCreatorDefinition.beanClass = proxyCreatorType
72-
parentCtx.beanFactory.registerBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME, proxyCreatorDefinition)
62+
if(springConfig.disable.aspectj.autoweaving) {
63+
"org.springframework.aop.config.internalAutoProxyCreator"(GroovyAwareInfrastructureAdvisorAutoProxyCreator)
64+
}
65+
else {
66+
"org.springframework.aop.config.internalAutoProxyCreator"(GroovyAwareAspectJAwareAdvisorAutoProxyCreator)
67+
}
7368

7469
// Allow the use of Spring annotated components
7570
context.'annotation-config'()

src/java/org/codehaus/groovy/grails/support/MockApplicationContext.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.springframework.util.AntPathMatcher;
5151
import org.springframework.util.PathMatcher;
5252
import org.springframework.web.context.WebApplicationContext;
53-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
5453

5554
public class MockApplicationContext extends GroovyObjectSupport implements WebApplicationContext {
5655

@@ -60,7 +59,6 @@ public class MockApplicationContext extends GroovyObjectSupport implements WebAp
6059
List<String> ignoredClassLocations = new ArrayList<String>();
6160
PathMatcher pathMatcher = new AntPathMatcher();
6261
ServletContext servletContext = new MockServletContext();
63-
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
6462

6563
public void registerMockBean(String name, Object instance) {
6664
beans.put(name, instance);
@@ -115,10 +113,6 @@ public ApplicationContext getParent() {
115113
throw new UnsupportedOperationException("Method not supported by implementation");
116114
}
117115

118-
BeanFactory getBeanFactory() {
119-
return beanFactory;
120-
}
121-
122116
public String getId() {
123117
return "MockApplicationContext";
124118
}
@@ -199,30 +193,26 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
199193

200194
public Object getBean(String name) throws BeansException {
201195
if (!beans.containsKey(name)) {
202-
if (!beanFactory.containsBean(name)) {
203-
throw new NoSuchBeanDefinitionException(name);
204-
} else {
205-
return beanFactory.getBean(name);
206-
}
207-
} else {
208-
return beans.get(name);
196+
throw new NoSuchBeanDefinitionException(name);
209197
}
198+
return beans.get(name);
210199
}
211200

212201
@SuppressWarnings("unchecked")
213202
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
214-
Object bean = getBean(name);
203+
if (!beans.containsKey(name)) {
204+
throw new NoSuchBeanDefinitionException( name);
205+
}
215206

216-
if (requiredType != null && !requiredType.isAssignableFrom(bean.getClass())) {
207+
if (requiredType != null && !requiredType.isAssignableFrom(beans.get(name).getClass())) {
217208
throw new NoSuchBeanDefinitionException(name);
218209
}
219210

220-
return (T)bean;
211+
return (T)beans.get(name);
221212
}
222213

223214
public <T> T getBean(Class<T> tClass) throws BeansException {
224215
final Map<String, T> map = getBeansOfType(tClass);
225-
map.putAll(beanFactory.getBeansOfType(tClass));
226216
if (map.isEmpty()) {
227217
throw new NoSuchBeanDefinitionException(tClass, "No bean found for type: " + tClass.getName());
228218
}
@@ -339,7 +329,7 @@ public boolean containsLocalBean(String arg0) {
339329
}
340330

341331
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
342-
return beanFactory;
332+
return new DefaultListableBeanFactory();
343333
}
344334
public ClassLoader getClassLoader() {
345335
return getClass().getClassLoader();

src/test/org/codehaus/groovy/grails/plugins/CoreGrailsPluginTests.groovy

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import org.codehaus.groovy.grails.commons.test.AbstractGrailsMockTests
66
import org.codehaus.groovy.grails.aop.framework.autoproxy.GroovyAwareAspectJAwareAdvisorAutoProxyCreator
77
import org.codehaus.groovy.grails.aop.framework.autoproxy.GroovyAwareInfrastructureAdvisorAutoProxyCreator
88

9-
import grails.spring.BeanBuilder
10-
119
class CoreGrailsPluginTests extends AbstractGrailsMockTests {
1210

1311
void testCorePlugin() {
@@ -117,37 +115,4 @@ class CoreGrailsPluginTests extends AbstractGrailsMockTests {
117115
assertEquals(1, appCtx.getBean('someTransactionalService').i)
118116
assertEquals(2, appCtx.getBean('nonTransactionalService').i)
119117
}
120-
121-
// See GRAILS-6790
122-
void testAopConfigurationIsEffective() {
123-
def pluginClass = gcl.loadClass("org.codehaus.groovy.grails.plugins.CoreGrailsPlugin")
124-
def plugin = new DefaultGrailsPlugin(pluginClass, ga)
125-
def springConfig = new WebRuntimeSpringConfiguration(ctx)
126-
127-
plugin.doWithRuntimeConfiguration(springConfig)
128-
129-
def bb = new BeanBuilder(ctx, springConfig, null)
130-
bb.beans {
131-
xmlns aop: "http://www.springframework.org/schema/aop"
132-
133-
aop.config("proxy-target-class": true) {
134-
135-
aspect(id: "myPlainJavaAspect-id", ref: "myAspect") {
136-
"before" method: "myBeforeAdvice", pointcut: "execution(* myMethod1(..))"
137-
}
138-
}
139-
140-
myInterfaceImpl(CoreGrailsPluginTestsAopConfig.MyInterfaceImpl)
141-
myAspect(CoreGrailsPluginTestsAopConfig.MyAspect)
142-
}
143-
144-
def appCtx = springConfig.getApplicationContext()
145-
def bean = appCtx.getBean("myInterfaceImpl")
146-
147-
// if we can call the following method, then the proxy was class based
148-
// because 'myMethod2' is not on the only interface this implements
149-
bean.myMethod2()
150-
}
151-
152-
153118
}

src/test/org/codehaus/groovy/grails/plugins/CoreGrailsPluginTestsAopConfig.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)