Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve scan base package #757

Open
wants to merge 10 commits into
base: 2.7.x
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,11 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertyResolver;

import java.util.Map;
import java.util.Set;

import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;
import static java.util.Collections.emptySet;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_BEAN_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_PREFIX;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_SCAN_PREFIX;
import static org.apache.dubbo.spring.boot.util.DubboUtils.RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME;
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

Expand All @@ -56,32 +41,6 @@
@AutoConfigureBefore(DubboRelaxedBindingAutoConfiguration.class)
public class DubboRelaxedBinding2AutoConfiguration {

public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnvironment environment) {
ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
Map<String, Object> dubboScanProperties = getSubProperties(environment.getPropertySources(), DUBBO_SCAN_PREFIX);
propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
}
};
ConfigurationPropertySources.attach(propertyResolver);
return propertyResolver;
}

/**
* The bean is used to scan the packages of Dubbo Service classes
*
* @param environment {@link Environment} instance
* @return non-null {@link Set}
* @since 2.7.8
*/
@ConditionalOnMissingBean(name = BASE_PACKAGES_BEAN_NAME)
@Bean(name = BASE_PACKAGES_BEAN_NAME)
public Set<String> dubboBasePackages(ConfigurableEnvironment environment) {
PropertyResolver propertyResolver = dubboScanBasePackagesPropertyResolver(environment);
return propertyResolver.getProperty(BASE_PACKAGES_PROPERTY_NAME, Set.class, emptySet());
}

@ConditionalOnMissingBean(name = RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME, value = ConfigurationBeanBinder.class)
@Bean(RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME)
@Scope(scopeName = SCOPE_PROTOTYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package org.apache.dubbo.spring.boot.autoconfigure;

import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceClassPostProcessor;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceClassPostProcessor;
import org.apache.dubbo.spring.boot.util.DubboUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.ObjectProvider;
Expand All @@ -29,15 +30,9 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ClassUtils;

import java.util.Map;
import java.util.Set;

import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_BEAN_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -54,10 +49,6 @@
@PropertySource(value = "classpath:/dubbo.properties")
public class DubboRelaxedBinding2AutoConfigurationTest {

@Autowired
@Qualifier(BASE_PACKAGES_BEAN_NAME)
private Set<String> packagesToScan;

@Autowired
@Qualifier(RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME)
private ConfigurationBeanBinder dubboConfigBinder;
Expand All @@ -71,26 +62,18 @@ public class DubboRelaxedBinding2AutoConfigurationTest {
@Autowired
private Environment environment;

@Autowired
private Map<String, Environment> environments;

@Test
public void testBeans() {

assertEquals(1, DubboUtils.getScanBasePackage(environment).size());

assertTrue(ClassUtils.isAssignableValue(BinderDubboConfigBinder.class, dubboConfigBinder));

assertNotNull(serviceClassPostProcessor);
assertNotNull(serviceClassPostProcessor.getIfAvailable());
assertNotNull(referenceAnnotationBeanPostProcessor);
assertNotNull(referenceAnnotationBeanPostProcessor.getIfAvailable());

assertNotNull(environment);
assertNotNull(environments);


assertEquals(1, environments.size());

assertTrue(environments.containsValue(environment));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@
import org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener;
import org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;

import org.apache.dubbo.spring.boot.util.DubboUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.Set;

import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_BEAN_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_PREFIX;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_SCAN_PREFIX;
Expand All @@ -65,15 +61,12 @@ public class DubboAutoConfiguration implements ApplicationContextAware, BeanDefi
/**
* Creates {@link ServiceClassPostProcessor} Bean
*
* @param packagesToScan the packages to scan
* @return {@link ServiceClassPostProcessor}
*/
@ConditionalOnProperty(prefix = DUBBO_SCAN_PREFIX, name = BASE_PACKAGES_PROPERTY_NAME)
@ConditionalOnBean(name = BASE_PACKAGES_BEAN_NAME)
@Bean
public ServiceClassPostProcessor serviceClassPostProcessor(@Qualifier(BASE_PACKAGES_BEAN_NAME)
Set<String> packagesToScan) {
return new ServiceClassPostProcessor(packagesToScan);
public ServiceClassPostProcessor serviceClassPostProcessor(Environment environment) {
return new ServiceClassPostProcessor(DubboUtils.getScanBasePackage(environment));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,11 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;

import java.util.Set;

import static java.util.Collections.emptySet;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_BEAN_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_PREFIX;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_SCAN_PREFIX;
import static org.apache.dubbo.spring.boot.util.DubboUtils.RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME;
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

Expand All @@ -45,24 +36,6 @@
@Configuration
public class DubboRelaxedBindingAutoConfiguration {

public PropertyResolver dubboScanBasePackagesPropertyResolver(Environment environment) {
return new RelaxedPropertyResolver(environment, DUBBO_SCAN_PREFIX);
}

/**
* The bean is used to scan the packages of Dubbo Service classes
*
* @param environment {@link Environment} instance
* @return non-null {@link Set}
* @since 2.7.8
*/
@ConditionalOnMissingBean(name = BASE_PACKAGES_BEAN_NAME)
@Bean(name = BASE_PACKAGES_BEAN_NAME)
public Set<String> dubboBasePackages(Environment environment) {
PropertyResolver propertyResolver = dubboScanBasePackagesPropertyResolver(environment);
return propertyResolver.getProperty(BASE_PACKAGES_PROPERTY_NAME, Set.class, emptySet());
}

@ConditionalOnMissingBean(name = RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME, value = ConfigurationBeanBinder.class)
@Bean(RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME)
@Scope(scopeName = SCOPE_PROTOTYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.ContextIdApplicationContextInitializer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;

import java.util.Collections;
Expand All @@ -32,6 +34,8 @@
import java.util.SortedMap;
import java.util.TreeMap;

import static java.util.Collections.emptySet;

/**
* The utilities class for Dubbo
*
Expand Down Expand Up @@ -212,4 +216,20 @@ public static SortedMap<String, Object> filterDubboProperties(ConfigurableEnviro
return Collections.unmodifiableSortedMap(dubboProperties);
}

public static Set<String> getScanBasePackage(Environment environment) {
// spring-boot 2.x Environment support relaxed properties
String name = DUBBO_SCAN_PREFIX + BASE_PACKAGES_PROPERTY_NAME;
Set<String> packagesToScan = environment.getProperty(name, Set.class, emptySet());

//read relaxed properties compatible with spring-boot 1.x
if (packagesToScan == null || packagesToScan.isEmpty()) {
try {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, DUBBO_SCAN_PREFIX);
packagesToScan = propertyResolver.getProperty(BASE_PACKAGES_PROPERTY_NAME, Set.class, emptySet());
} catch (NoClassDefFoundError ex) {
//ignore
}
}
return packagesToScan;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@RunWith(SpringRunner.class)
@TestPropertySource(
properties = {
"dubbo.application.name = dubbo-demo-application",
"dubbo.application.NAME = dubbo-demo-application",
"dubbo.module.name = dubbo-demo-module",
"dubbo.registry.address = zookeeper://192.168.99.100:32770",
"dubbo.protocol.name=dubbo",
Expand Down