Pre-register default bindings for channels

- This allows resolution of any binding properties specified with `_` instead of `.`
- remove cast and check for ConfigurableEnvironment
- remove the duplicate defaulting channel binding code

Remove unused import
This commit is contained in:
Marius Bogoevici
2015-08-25 14:07:45 -04:00
committed by Ilayaperumal Gopinathan
parent 6de7f7cb33
commit 83123c3b97
3 changed files with 33 additions and 9 deletions

View File

@@ -22,7 +22,6 @@ import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -37,9 +36,6 @@ public class ChannelBindingProperties {
public static final String PATH = "path";
@Value("${spring.application.name:}")
private String applicationName;
private Properties consumerProperties = new Properties();
private Properties producerProperties = new Properties();
@@ -85,8 +81,8 @@ public class ChannelBindingProperties {
}
}
}
// the default path of the binding is the channel name itself
return (StringUtils.hasText(applicationName) ? applicationName + "." : "") + channelName;
// just return the channel name if not found
return channelName;
}
public String getTapChannelName(String channelName) {

View File

@@ -18,11 +18,16 @@ package org.springframework.cloud.stream.config;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.cloud.stream.utils.MessageChannelBeanDefinitionRegistryUtils;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
@@ -31,19 +36,36 @@ import org.springframework.util.MultiValueMap;
* @author Marius Bogoevici
* @author Dave Syer
*/
public class ModuleRegistrar implements ImportBeanDefinitionRegistrar {
public class ModuleRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {
public static final String SPRING_CLOUD_STREAM_BINDINGS_PREFIX = "spring.cloud.stream.bindings";
private ConfigurableEnvironment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = (ConfigurableEnvironment) environment;
}
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
EnableModule.class.getName(), false);
List<String> registeredChannelNames = new ArrayList<>();
for (Class<?> type : collectClasses(attributes.get("value"))) {
MessageChannelBeanDefinitionRegistryUtils.registerChannelBeanDefinitions(type, registry);
registeredChannelNames.addAll(MessageChannelBeanDefinitionRegistryUtils.registerChannelBeanDefinitions(type, registry));
MessageChannelBeanDefinitionRegistryUtils.registerChannelsQualifiedBeanDefinitions(
ClassUtils.resolveClassName(metadata.getClassName(), null), type,
registry);
}
Properties defaultChannelNameProperties = new Properties();
for (String registeredChannelName : registeredChannelNames) {
defaultChannelNameProperties.put(SPRING_CLOUD_STREAM_BINDINGS_PREFIX + "." + registeredChannelName,
"${spring.application.name:spring.cloud.stream}" + "." + registeredChannelName);
}
environment.getPropertySources().addLast(
new PropertiesPropertySource("default-spring-cloud-stream-channel-bindings", defaultChannelNameProperties));
}
private List<Class<?>> collectClasses(List<Object> list) {

View File

@@ -18,6 +18,8 @@ package org.springframework.cloud.stream.utils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
@@ -85,8 +87,9 @@ public abstract class MessageChannelBeanDefinitionRegistryUtils {
registry.registerBeanDefinition(name, rootBeanDefinition);
}
public static void registerChannelBeanDefinitions(Class<?> type,
public static List<String> registerChannelBeanDefinitions(Class<?> type,
final BeanDefinitionRegistry registry) {
final List<String> channelNames = new ArrayList<>();
ReflectionUtils.doWithMethods(type, new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException,
@@ -95,15 +98,18 @@ public abstract class MessageChannelBeanDefinitionRegistryUtils {
if (input != null) {
String name = getName(input, method);
registerInputChannelBeanDefinition(input.value(), name, registry);
channelNames.add(name);
}
Output output = AnnotationUtils.findAnnotation(method, Output.class);
if (output != null) {
String name = getName(output, method);
registerOutputChannelBeanDefinition(output.value(), name, registry);
channelNames.add(name);
}
}
});
return channelNames;
}
public static void registerChannelsQualifiedBeanDefinitions(Class<?> parent, Class<?> type,