Update SpringConfiguration to support beans by type
Issue: SPR-10605
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.web.socket.server.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import javax.websocket.server.ServerEndpointConfig.Configurator;
|
||||
|
||||
@@ -24,6 +27,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@@ -48,7 +52,13 @@ public class SpringConfigurator extends Configurator {
|
||||
|
||||
private static Log logger = LogFactory.getLog(SpringConfigurator.class);
|
||||
|
||||
private static final Map<String, Map<Class<?>, String>> cache =
|
||||
new ConcurrentHashMap<String, Map<Class<?>, String>>();
|
||||
|
||||
private static final String NO_VALUE = ObjectUtils.identityToString(new Object());
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
|
||||
|
||||
@@ -77,10 +87,44 @@ public class SpringConfigurator extends Configurator {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
beanName = getBeanNameByType(wac, endpointClass);
|
||||
if (beanName != null) {
|
||||
return (T) wac.getBean(beanName);
|
||||
}
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
|
||||
}
|
||||
return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
|
||||
}
|
||||
|
||||
private String getBeanNameByType(WebApplicationContext wac, Class<?> endpointClass) {
|
||||
|
||||
String wacId = wac.getId();
|
||||
|
||||
Map<Class<?>, String> beanNamesByType = cache.get(wacId);
|
||||
if (beanNamesByType == null) {
|
||||
beanNamesByType = new ConcurrentHashMap<Class<?>, String>();
|
||||
cache.put(wacId, beanNamesByType);
|
||||
}
|
||||
|
||||
if (!beanNamesByType.containsKey(endpointClass)) {
|
||||
String[] names = wac.getBeanNamesForType(endpointClass);
|
||||
if (names.length == 1) {
|
||||
beanNamesByType.put(endpointClass, names[0]);
|
||||
}
|
||||
else {
|
||||
beanNamesByType.put(endpointClass, NO_VALUE);
|
||||
if (names.length > 1) {
|
||||
String message = "Found multiple @ServerEndpoint's of type " + endpointClass + ", names=" + names;
|
||||
logger.error(message);
|
||||
throw new IllegalStateException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String beanName = beanNamesByType.get(endpointClass);
|
||||
return NO_VALUE.equals(beanName) ? null : beanName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user