Skip to content

Commit 45f4578

Browse files
committed
fix for GRAILS-5564 "Service scopes not working"
1 parent 71cce24 commit 45f4578

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package org.codehaus.groovy.grails.beans.factory;
2+
3+
/*
4+
* Copyright 2002-2008 the original author or authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.lang.annotation.Annotation;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.beans.BeansException;
24+
import org.springframework.beans.factory.ListableBeanFactory;
25+
import org.springframework.beans.factory.config.BeanDefinition;
26+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
27+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
28+
import org.springframework.core.annotation.AnnotationUtils;
29+
import org.springframework.util.Assert;
30+
31+
/**
32+
* This class is a fork of the Spring 2.5.6 GenericBeanFactoryAccess class that was removed
33+
* from Spring 3.0
34+
*
35+
* @author Rob Harrop
36+
* @author Juergen Hoeller
37+
* @since 2.0
38+
*/
39+
public class GenericBeanFactoryAccessor {
40+
41+
/**
42+
* The {@link ListableBeanFactory} being wrapped.
43+
*/
44+
private final ListableBeanFactory beanFactory;
45+
46+
47+
/**
48+
* Constructs a <code>GenericBeanFactoryAccessor</code> that wraps the supplied {@link ListableBeanFactory}.
49+
*/
50+
public GenericBeanFactoryAccessor(ListableBeanFactory beanFactory) {
51+
Assert.notNull(beanFactory, "Bean factory must not be null");
52+
this.beanFactory = beanFactory;
53+
}
54+
55+
/**
56+
* Return the wrapped {@link ListableBeanFactory}.
57+
*/
58+
public final ListableBeanFactory getBeanFactory() {
59+
return this.beanFactory;
60+
}
61+
62+
63+
/**
64+
* @see org.springframework.beans.factory.BeanFactory#getBean(String)
65+
*/
66+
public <T> T getBean(String name) throws BeansException {
67+
return (T) this.beanFactory.getBean(name);
68+
}
69+
70+
/**
71+
* @see org.springframework.beans.factory.BeanFactory#getBean(String, Class)
72+
*/
73+
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
74+
return (T) this.beanFactory.getBean(name, requiredType);
75+
}
76+
77+
/**
78+
* @see ListableBeanFactory#getBeansOfType(Class)
79+
*/
80+
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
81+
return this.beanFactory.getBeansOfType(type);
82+
}
83+
84+
/**
85+
* @see ListableBeanFactory#getBeansOfType(Class, boolean, boolean)
86+
*/
87+
public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
88+
throws BeansException {
89+
90+
return this.beanFactory.getBeansOfType(type, includeNonSingletons, allowEagerInit);
91+
}
92+
93+
/**
94+
* Find all beans whose <code>Class</code> has the supplied {@link Annotation} type.
95+
* @param annotationType the type of annotation to look for
96+
* @return a Map with the matching beans, containing the bean names as
97+
* keys and the corresponding bean instances as values
98+
*/
99+
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
100+
Map<String, Object> results = new LinkedHashMap<String, Object>();
101+
for (String beanName : this.beanFactory.getBeanNamesForType(Object.class)) {
102+
if (findAnnotationOnBean(beanName, annotationType) != null) {
103+
results.put(beanName, this.beanFactory.getBean(beanName));
104+
}
105+
}
106+
return results;
107+
}
108+
109+
/**
110+
* Find a {@link Annotation} of <code>annotationType</code> on the specified
111+
* bean, traversing its interfaces and super classes if no annotation can be
112+
* found on the given class itself, as well as checking its raw bean class
113+
* if not found on the exposed bean reference (e.g. in case of a proxy).
114+
* @param beanName the name of the bean to look for annotations on
115+
* @param annotationType the annotation class to look for
116+
* @return the annotation of the given type found, or <code>null</code>
117+
* @see org.springframework.core.annotation.AnnotationUtils#findAnnotation(Class, Class)
118+
*/
119+
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
120+
Class<?> handlerType = this.beanFactory.getType(beanName);
121+
A ann = AnnotationUtils.findAnnotation(handlerType, annotationType);
122+
if (ann == null && this.beanFactory instanceof ConfigurableBeanFactory &&
123+
this.beanFactory.containsBeanDefinition(beanName)) {
124+
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) this.beanFactory;
125+
BeanDefinition bd = cbf.getMergedBeanDefinition(beanName);
126+
if (bd instanceof AbstractBeanDefinition) {
127+
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
128+
if (abd.hasBeanClass()) {
129+
Class<?> beanClass = abd.getBeanClass();
130+
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
131+
}
132+
}
133+
}
134+
return ann;
135+
}
136+
137+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.codehaus.groovy.grails.plugins
1717

18+
import org.codehaus.groovy.grails.beans.factory.GenericBeanFactoryAccessor;
1819
import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
1920

2021
import grails.util.GrailsUtil
@@ -43,8 +44,9 @@ public class ValidationGrailsPlugin {
4344
application.config?.grails?.validateable?.classes?.each {
4445
validateables << it
4546
}
46-
47-
for(entry in ctx.getBeansWithAnnotation(Validateable)) {
47+
48+
def accessor = new GenericBeanFactoryAccessor(ctx)
49+
for(entry in accessor.getBeansWithAnnotation(Validateable)) {
4850
Class validateable = entry?.value?.class
4951
if(validateable)
5052
validateables << validateable

0 commit comments

Comments
 (0)