INT-3328: Add EnablePublisher Annotation

JIRA: https://jira.spring.io/browse/INT-3328

INT-3328: Polishing
This commit is contained in:
Artem Bilan
2014-03-17 18:24:18 +02:00
committed by Gary Russell
parent a8c8a4fed5
commit 23a26ca38e
8 changed files with 157 additions and 20 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* Provides the registration for the {@link org.springframework.integration.aop.PublisherAnnotationBeanPostProcessor}
* to allow the use of the {@link org.springframework.integration.annotation.Publisher} annotation.
* In addition the {@code default-publisher-channel} name has to be configured as the {@code value} of this annotation.
* <p>
* Note: the {@link org.springframework.integration.annotation.Publisher} annotation is enabled by default via
* {@link EnableIntegration} processing, but there is no hook to configure the {@code default-publisher-channel}.
*
* @author Artem Bilan
* @since 4.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(PublisherRegistrar.class)
public @interface EnablePublisher {
/**
* @return the {@code default-publisher-channel} name.
*/
String value();
}

View File

@@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
@@ -51,7 +50,6 @@ import org.springframework.integration.expression.IntegrationEvaluationContextAw
import org.springframework.integration.support.DefaultMessageBuilderFactory;
import org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* {@link ImportBeanDefinitionRegistrar} implementation that configures integration infrastructure.
@@ -302,20 +300,7 @@ public class IntegrationRegistrar implements ImportBeanDefinitionRegistrar, Bean
registry.registerBeanDefinition(IntegrationContextUtils.MESSAGING_ANNOTATION_POSTPROCESSOR_NAME, builder.getBeanDefinition());
}
if (!registry.containsBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PublisherAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
Map<String, Object> attrs = meta.getAnnotationAttributes(EnableIntegration.class.getName());
String defaultPublisherChannel = (String) attrs.get("defaultPublisherChannel");
if (StringUtils.hasText(defaultPublisherChannel)) {
builder.addPropertyReference("defaultChannel", defaultPublisherChannel);
}
registry.registerBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME, builder.getBeanDefinition());
}
new PublisherRegistrar().registerBeanDefinitions(meta, registry);
}
/**

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.config;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.integration.aop.PublisherAnnotationBeanPostProcessor;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.util.StringUtils;
/**
* @author Artem Bilan
* @since 4.0
*/
public class PublisherRegistrar implements ImportBeanDefinitionRegistrar {
private static final Log logger = LogFactory.getLog(PublisherRegistrar.class);
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnablePublisher.class.getName());
if (annotationAttributes == null) {
return;
}
String value = (String) annotationAttributes.get("value");
if (!registry.containsBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PublisherAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
if (StringUtils.hasText(value)) {
builder.addPropertyReference("defaultChannel", value);
if (logger.isInfoEnabled()) {
logger.info("Setting '@Publisher' default-output-channel to '" + value + "'.");
}
}
registry.registerBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME, builder.getBeanDefinition());
}
else {
BeanDefinition beanDefinition = registry.getBeanDefinition(IntegrationContextUtils.PUBLISHER_ANNOTATION_POSTPROCESSOR_NAME);
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
RuntimeBeanReference defaultChannel = (RuntimeBeanReference) propertyValues.getPropertyValue("defaultChannel").getValue();
if (StringUtils.hasText(value)) {
if (defaultChannel == null) {
propertyValues.addPropertyValue("defaultChannel", new RuntimeBeanReference(value));
if (logger.isInfoEnabled()) {
logger.info("Setting '@Publisher' default-output-channel to '" + value + "'.");
}
}
else if (!value.equals(defaultChannel.getBeanName())) {
throw new BeanDefinitionStoreException("When more than one enable publisher definition " +
"(@EnablePublisher or <annotation-config>)" +
" is found in the context, they all must have the same 'default-publisher-channel' value.");
}
}
}
}
}

View File

@@ -41,7 +41,7 @@ public class AnnotationConfigParser implements BeanDefinitionParser {
@Override
public Map<String, Object> getAnnotationAttributes(String annotationType) {
return Collections.<String, Object> singletonMap("defaultPublisherChannel", element.getAttribute("default-publisher-channel"));
return Collections.<String, Object> singletonMap("value", element.getAttribute("default-publisher-channel"));
}
}, parserContext.getRegistry());

View File

@@ -8,4 +8,6 @@
<message-history tracked-components="publishedChannel,input,*AnnotationTestService*"/>
<annotation-config default-publisher-channel="publishedChannel"/>
</beans:beans>

View File

@@ -47,6 +47,7 @@ import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.EnableMessageHistory;
import org.springframework.integration.config.EnablePublisher;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.history.MessageHistoryConfigurer;
import org.springframework.integration.support.MessageBuilder;
@@ -152,6 +153,7 @@ public class EnableIntegrationTests {
@EnableIntegration
@ImportResource("classpath:org/springframework/integration/configuration/EnableIntegrationTests-context.xml")
@EnableMessageHistory("${message.history.tracked.components}")
@EnablePublisher("publishedChannel")
public static class ContextConfiguration2 {
@Bean
@@ -175,7 +177,7 @@ public class EnableIntegrationTests {
public static class AnnotationTestService {
@ServiceActivator(inputChannel = "input", outputChannel = "output")
@Publisher(channel = "publishedChannel")
@Publisher
@Payload("#args[0].toLowerCase()")
public String handle(String payload) {
return payload.toUpperCase();

View File

@@ -345,12 +345,20 @@
components to be declared once only, in the parent context.
</para>
<para>
The <code>@IntegrationComponentScan</code> annotation has also been introduced to permit classpath
The <code>@IntegrationComponentScan</code> annotation has also been introduced to permit classpath
scanning. This annotation plays a similar role as the standard Spring Framework <code>@ComponentScan</code> annotation,
but it is restricted just to Spring Integration specific components and annotations,
which aren't reachable by the standard Spring Framework component scan mechanism.
For example <xref linkend="messaging-gateway-annotation"/>.
</para>
</para>
<para>
The <code>@EnablePublisher</code> annotation has been introduced to register a
<classname>PublisherAnnotationBeanPostProcessor</classname> bean and configure the <code>default-publisher-channel</code>
for those <classname>@Publisher</classname> annotations which are provided without a <code>channel</code> attribute.
If more than one <code>@EnablePublisher</code> annotation is found, they must all have the same value
for the default channel.
See <xref linkend="publisher-annotation"/> for more information.
</para>
</section>
</chapter>

View File

@@ -65,6 +65,14 @@
>Spring Boot - AutoConfigure</ulink>.
</para>
</section>
<section id="4.0-enable-publisher">
<title>@EnablePublisher</title>
<para>
The <code>@EnablePublisher</code> annotation has been added, to allow the specification of a
<code>default-publisher-channel</code> for <classname>@Publisher</classname> annotations.
See <xref linkend="enable-integration"/> for more information.
</para>
</section>
<section id="4.0-redis-cms">
<title>Redis Channel Message Stores</title>
<para>