自行下载dubbo源码 :git clone https://github.com/apache/dubbo.git 2.7.8
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.provider") //@1
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
}
@EnableDubboConfig
@DubboComponentScan
public @interface EnableDubbo {
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Import(DubboConfigConfigurationRegistrar.class)
public @interface EnableDubboConfig {
/**
* It indicates whether binding to multiple Spring Beans.
*
* @return the default value is <code>true</code>
* @revised 2.5.9
*/
boolean multiple() default true;
}
public class DubboConfigConfigurationRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
importingClassMetadata.getAnnotationAttributes(EnableDubboConfig.class.getName()));
boolean multiple = attributes.getBoolean("multiple");
// Single Config Bindings
registerBeans(registry, DubboConfigConfiguration.Single.class);
if (multiple) { // Since 2.6.6 https://github.com/apache/dubbo/issues/3193
registerBeans(registry, DubboConfigConfiguration.Multiple.class);
}
// Since 2.7.6
registerCommonBeans(registry);
}
}
执行到此时启动日志
[10/07/20 13:40:47:111 CST] main INFO annotation.AnnotationConfigApplicationContext: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4973813a: startup date [Fri Jul 10 13:40:47 CST 2020]; root of context hierarchy
/**
* Dubbo Bean utilities class
*
* @since 2.7.6
*/
public interface DubboBeanUtils {
/**
* Register the common beans
*
* @param registry {@link BeanDefinitionRegistry}
* @see ReferenceAnnotationBeanPostProcessor
* @see DubboConfigDefaultPropertyValueBeanPostProcessor
* @see DubboConfigAliasPostProcessor
* @see DubboLifecycleComponentApplicationListener
* @see DubboBootstrapApplicationListener
*/
static void registerCommonBeans(BeanDefinitionRegistry registry) {
// Since 2.5.7 Register @Reference Annotation Bean Processor as an infrastructure Bean
registerInfrastructureBean(registry, ReferenceAnnotationBeanPostProcessor.BEAN_NAME,
ReferenceAnnotationBeanPostProcessor.class); //如果不存在则注册
// Since 2.7.4 [Feature] https://github.com/apache/dubbo/issues/5093
registerInfrastructureBean(registry, DubboConfigAliasPostProcessor.BEAN_NAME,
DubboConfigAliasPostProcessor.class);
// Since 2.7.5 Register DubboLifecycleComponentApplicationListener as an infrastructure Bean
registerInfrastructureBean(registry, DubboLifecycleComponentApplicationListener.BEAN_NAME,
DubboLifecycleComponentApplicationListener.class);
// Since 2.7.4 Register DubboBootstrapApplicationListener as an infrastructure Bean
registerInfrastructureBean(registry, DubboBootstrapApplicationListener.BEAN_NAME,
DubboBootstrapApplicationListener.class);
// Since 2.7.6 Register DubboConfigDefaultPropertyValueBeanPostProcessor as an infrastructure Bean
registerInfrastructureBean(registry, DubboConfigDefaultPropertyValueBeanPostProcessor.BEAN_NAME,
DubboConfigDefaultPropertyValueBeanPostProcessor.class);
}
}
[10/07/20 13:56:43:456 CST] main INFO util.BeanRegistrar: The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor];
[10/07/20 13:58:39:676 CST] main INFO util.BeanRegistrar: The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigAliasPostProcessor];
[10/07/20 13:59:24:791 CST] main INFO util.BeanRegistrar: The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener];
[10/07/20 14:02:33:335 CST] main INFO util.BeanRegistrar: The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener];
[10/07/20 14:02:39:457 CST] main INFO util.BeanRegistrar: The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.config.DubboConfigDefaultPropertyValueBeanPostProcessor];
has been registered.
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
/**
* The {@link ImportBeanDefinitionRegistrar Registrar class} for {@link EnableConfigurationBeanBindings}
*
* @since 1.0.4
*/
public class ConfigurationBeanBindingsRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private ConfigurableEnvironment environment;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
importingClassMetadata.getAnnotationAttributes(EnableConfigurationBeanBindings.class.getName()));
AnnotationAttributes[] annotationAttributes = attributes.getAnnotationArray("value");
ConfigurationBeanBindingRegistrar registrar = new ConfigurationBeanBindingRegistrar();
registrar.setEnvironment(environment);
for (AnnotationAttributes element : annotationAttributes) {
registrar.registerConfigurationBeanDefinitions(element, registry);
}
}
@Override
public void setEnvironment(Environment environment) {
Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
this.environment = (ConfigurableEnvironment) environment;
}
}
annotationAttributes
annotationAttributes = {AnnotationAttributes[11]@1341}
0 = {AnnotationAttributes@1343} size = 5
"type" -> {Class@1362} "class org.apache.dubbo.config.ApplicationConfig"
"prefix" -> "dubbo.application"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
1 = {AnnotationAttributes@1344} size = 5
"type" -> {Class@1386} "class org.apache.dubbo.config.ModuleConfig"
"prefix" -> "dubbo.module"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
2 = {AnnotationAttributes@1345} size = 5
"type" -> {Class@1396} "class org.apache.dubbo.config.RegistryConfig"
"prefix" -> "dubbo.registry"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
3 = {AnnotationAttributes@1346} size = 5
"type" -> {Class@1406} "class org.apache.dubbo.config.ProtocolConfig"
"prefix" -> "dubbo.protocol"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
4 = {AnnotationAttributes@1347} size = 5
"type" -> {Class@1416} "class org.apache.dubbo.config.MonitorConfig"
"prefix" -> "dubbo.monitor"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
5 = {AnnotationAttributes@1348} size = 5
"type" -> {Class@1436} "class org.apache.dubbo.config.ProviderConfig"
"prefix" -> "dubbo.provider"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
6 = {AnnotationAttributes@1349} size = 5
"type" -> {Class@1426} "class org.apache.dubbo.config.ConsumerConfig"
"prefix" -> "dubbo.consumer"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
7 = {AnnotationAttributes@1350} size = 5
"type" -> {Class@1446} "class org.apache.dubbo.config.spring.ConfigCenterBean"
"prefix" -> "dubbo.config-center"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
8 = {AnnotationAttributes@1351} size = 5
"type" -> {Class@1456} "class org.apache.dubbo.config.MetadataReportConfig"
"prefix" -> "dubbo.metadata-report"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
9 = {AnnotationAttributes@1352} size = 5
"type" -> {Class@1466} "class org.apache.dubbo.config.MetricsConfig"
"prefix" -> "dubbo.metrics"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
10 = {AnnotationAttributes@1353} size = 5
"type" -> {Class@1476} "class org.apache.dubbo.config.SslConfig"
"prefix" -> "dubbo.ssl"
"multiple" -> {Boolean@1366} false
"ignoreUnknownFields" -> {Boolean@1368} true
"ignoreInvalidFields" -> {Boolean@1368} true
ConfigurationBeanBindingRegistrar
registerBeanDefinitions ⇒ registerConfigurationBeanDefinitions ⇒ registerConfigurationBeans
ServiceClassPostProcessor
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
// @since 2.7.5
registerInfrastructureBean(registry, DubboBootstrapApplicationListener.BEAN_NAME, DubboBootstrapApplicationListener.class);
Set<String> resolvedPackagesToScan = resolvePackagesToScan(packagesToScan);
if (!CollectionUtils.isEmpty(resolvedPackagesToScan)) {
registerServiceBeans(resolvedPackagesToScan, registry);
} else {
if (logger.isWarnEnabled()) {
logger.warn("packagesToScan is empty , ServiceBean registry will be ignored!");
}
}
}
[10/07/20 14:29:23:719 CST] main INFO annotation.ServiceAnnotationBeanPostProcessor: [DUBBO] BeanNameGenerator bean can't be found in BeanFactory with name [org.springframework.context.annotation.internalConfigurationBeanNameGenerator], dubbo version: , current host: 192.168.1.188
[10/07/20 14:29:23:719 CST] main INFO annotation.ServiceAnnotationBeanPostProcessor: [DUBBO] BeanNameGenerator will be a instance of org.springframework.context.annotation.AnnotationBeanNameGenerator , it maybe a potential problem on bean name generation., dubbo version: , current host: 192.168.1.188
[10/07/20 14:29:24:113 CST] main INFO annotation.ServiceAnnotationBeanPostProcessor: [DUBBO] The BeanDefinition[Root bean: class [org.apache.dubbo.config.spring.ServiceBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] of ServiceBean has been registered with name : ServiceBean:org.apache.dubbo.demo.DemoService, dubbo version: , current host: 192.168.1.188
[10/07/20 14:29:24:114 CST] main INFO annotation.ServiceAnnotationBeanPostProcessor: [DUBBO] 1 annotated Dubbo's @Service Components { [Bean definition with name 'demoServiceImpl': Generic bean: class [org.apache.dubbo.demo.provider.DemoServiceImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/Users/file/srccode/dubbo/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-provider/target/classes/org/apache/dubbo/demo/provider/DemoServiceImpl.class]] } were scanned under package[org.apache.dubbo.demo.provider], dubbo version: , current host: 192.168.1.188
DubboConfigAliasPostProcessor
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AbstractConfig) {
String id = ((AbstractConfig) bean).getId();
if (hasText(id) // id MUST be present in AbstractConfig
&& !nullSafeEquals(id, beanName) // id MUST NOT be equal to bean name
&& !hasAlias(registry, beanName, id)) { // id MUST NOT be present in AliasRegistry
registry.registerAlias(beanName, id);
}
}
return bean;
}
DubboLifecycleComponentApplicationListener
@Override
public void onApplicationContextEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent) {
onContextRefreshedEvent((ContextRefreshedEvent) event);
} else if (event instanceof ContextClosedEvent) {
onContextClosedEvent((ContextClosedEvent) event);
}
}
private void onContextRefreshedEvent(ContextRefreshedEvent event) {
dubboBootstrap.start();
}
private void onContextClosedEvent(ContextClosedEvent event) {
dubboBootstrap.stop();
}
@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
}
DubboBootstrap.start