Add @IntegrationComponentScan auto-configuration

Update Spring Integration auto-configuration so that
`@IntegrationComponentScan` from `AutoConfigurationPackages` is
implicitly applied. Prior to this commit `@MessagingGateway` interfaces
would only get picked up if `@IntegrationComponentScan` was added
alongside with the `@SpringBootApplication`.

Fixes gh-2037
Closes gh-7718
This commit is contained in:
Artem Bilan 2016-12-21 09:28:08 -05:00 committed by Phillip Webb
parent 02917ff00a
commit a79f71cbe8
2 changed files with 81 additions and 7 deletions

View File

@ -16,11 +16,15 @@
package org.springframework.boot.autoconfigure.integration;
import java.util.Map;
import javax.management.MBeanServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -31,9 +35,15 @@ import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.EnableIntegrationManagement;
import org.springframework.integration.config.IntegrationComponentScanRegistrar;
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
@ -107,6 +117,46 @@ public class IntegrationAutoConfiguration {
@Configuration
@EnableIntegrationManagement(defaultCountsEnabled = "true", defaultStatsEnabled = "true")
protected static class EnableIntegrationManagementConfiguration {
}
}
@ConditionalOnMissingBean(GatewayProxyFactoryBean.class)
@Import(AutoIntegrationComponentScanRegistrar.class)
protected static class IntegrationComponentScanAutoConfiguration {
}
private static class AutoIntegrationComponentScanRegistrar
extends IntegrationComponentScanRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
final BeanDefinitionRegistry registry) {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(
IntegrationComponentScanConfiguration.class, true) {
@Override
public Map<String, Object> getAnnotationAttributes(
String annotationName) {
Map<String, Object> annotationAttributes = super.getAnnotationAttributes(
annotationName);
if (IntegrationComponentScan.class.getName().equals(annotationName)) {
BeanFactory beanFactory = (BeanFactory) registry;
if (AutoConfigurationPackages.has(beanFactory)) {
annotationAttributes.put("value",
AutoConfigurationPackages.get(beanFactory));
}
}
return annotationAttributes;
}
};
super.registerBeanDefinitions(metadata, registry);
}
@IntegrationComponentScan
private class IntegrationComponentScanConfiguration {
}

View File

@ -30,6 +30,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.gateway.RequestReplyExchanger;
import org.springframework.integration.support.channel.HeaderChannelRegistry;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.jmx.export.MBeanExporter;
@ -61,24 +64,34 @@ public class IntegrationAutoConfigurationTests {
@Test
public void integrationIsAvailable() {
load();
assertThat(this.context.getBean(HeaderChannelRegistry.class)).isNotNull();
assertThat(this.context.getBean(TestGateway.class)).isNotNull();
assertThat(this.context.getBean(IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration.class))
.isNotNull();
}
@Test
public void explicitIntegrationComponentScan() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(IntegrationComponentScanConfiguration.class,
JmxAutoConfiguration.class,
IntegrationAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(TestGateway.class)).isNotNull();
assertThat(this.context.getBeansOfType(IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration.class))
.isEmpty();
}
@Test
public void parentContext() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(JmxAutoConfiguration.class,
IntegrationAutoConfiguration.class);
this.context.refresh();
load();
AnnotationConfigApplicationContext parent = this.context;
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(JmxAutoConfiguration.class,
IntegrationAutoConfiguration.class);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "SPRING_JMX_DEFAULT_DOMAIN=org.foo");
this.context.refresh();
assertThat(this.context.getBean(HeaderChannelRegistry.class)).isNotNull();
((ConfigurableApplicationContext) this.context.getParent()).close();
this.context.close();
}
@Test
@ -151,4 +164,15 @@ public class IntegrationAutoConfigurationTests {
}
@Configuration
@IntegrationComponentScan
static class IntegrationComponentScanConfiguration {
}
@MessagingGateway
public interface TestGateway extends RequestReplyExchanger {
}
}