Skip to content

Commit a7b0cc0

Browse files
committed
SpringBeanNameResolver failed to resolve beans using scoped proxies (fixes ocpsoft#170)
1 parent 4d9f431 commit a7b0cc0

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

‎integration-spring/src/main/java/org/ocpsoft/rewrite/spring/SpringBeanNameResolver.java‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616
package org.ocpsoft.rewrite.spring;
1717

18+
import java.util.HashSet;
1819
import java.util.Map;
20+
import java.util.Set;
1921

2022
import org.ocpsoft.logging.Logger;
2123
import org.ocpsoft.rewrite.el.spi.BeanNameResolver;
24+
import org.springframework.beans.factory.ListableBeanFactory;
2225
import org.springframework.web.context.ContextLoader;
2326
import org.springframework.web.context.WebApplicationContext;
2427

@@ -43,24 +46,47 @@ public String getBeanName(Class<?> clazz)
4346
}
4447

4548
// obtain a map of bean names
46-
Map<String, ?> beans = context.getBeansOfType(clazz);
49+
Set<String> beanNames = resolveBeanNames(context, clazz);
4750

4851
// no beans of that type, nothing we can do
49-
if (beans == null || beans.size() == 0) {
52+
if (beanNames == null || beanNames.size() == 0) {
5053
return null;
5154
}
5255

5356
// more than one result -> warn the user
54-
else if (beans.size() > 1) {
57+
else if (beanNames.size() > 1) {
5558
log.warn("Spring knows more than one bean of type [{}]", clazz.getName());
5659
return null;
5760
}
5861

5962
// exactly one result -> we got a name
6063
else {
61-
return beans.keySet().iterator().next();
64+
return beanNames.iterator().next();
6265
}
6366

6467
}
6568

69+
/**
70+
* Will ignore scoped proxy target bean names.
71+
*
72+
* @see https://github.com/ocpsoft/rewrite/issues/170
73+
*/
74+
private Set<String> resolveBeanNames(ListableBeanFactory beanFactory, Class<?> clazz)
75+
{
76+
77+
final Set<String> result = new HashSet<String>();
78+
79+
Map<String, ?> beanMap = beanFactory.getBeansOfType(clazz);
80+
if (beanMap != null) {
81+
for (String name : beanMap.keySet()) {
82+
if (name != null && !name.startsWith("scopedTarget.")) {
83+
result.add(name);
84+
}
85+
}
86+
}
87+
88+
return result;
89+
90+
}
91+
6692
}

0 commit comments

Comments
 (0)