DubboAutoConfiguration.java 4.3 KB
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.dubbo.spring.starter.autoconfigure;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor;
import org.apache.dubbo.config.spring.context.annotation.DubboConfigConfiguration;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
武汉红喜's avatar
武汉红喜 已提交
25 26 27 28

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
武汉红喜's avatar
武汉红喜 已提交
29 30
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
武汉红喜's avatar
武汉红喜 已提交
31
import org.springframework.boot.context.properties.EnableConfigurationProperties;
武汉红喜's avatar
武汉红喜 已提交
32 33
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
武汉红喜's avatar
武汉红喜 已提交
34 35
import org.springframework.context.annotation.Import;
import org.springframework.core.env.PropertyResolver;
武汉红喜's avatar
武汉红喜 已提交
36 37 38 39

import java.util.Set;

import static java.util.Collections.emptySet;
武汉红喜's avatar
武汉红喜 已提交
40
import static org.apache.dubbo.spring.starter.util.DubboUtils.*;
武汉红喜's avatar
武汉红喜 已提交
41 42 43 44 45

/**
 * Dubbo Auto {@link Configuration}
 *
 * @see Reference
武汉红喜's avatar
武汉红喜 已提交
46 47 48 49
 * @see Service
 * @see ServiceAnnotationBeanPostProcessor
 * @see ReferenceAnnotationBeanPostProcessor
 * @since 2.7.0
武汉红喜's avatar
武汉红喜 已提交
50
 */
武汉红喜's avatar
武汉红喜 已提交
51
@ConditionalOnProperty(prefix = DUBBO_PREFIX, name = "enabled", matchIfMissing = true)
武汉红喜's avatar
武汉红喜 已提交
52
@Configuration
武汉红喜's avatar
武汉红喜 已提交
53 54
@AutoConfigureAfter(DubboRelaxedBindingAutoConfiguration.class)
@EnableConfigurationProperties(DubboConfigurationProperties.class)
武汉红喜's avatar
武汉红喜 已提交
55 56 57 58 59
public class DubboAutoConfiguration {

    /**
     * Creates {@link ServiceAnnotationBeanPostProcessor} Bean
     *
武汉红喜's avatar
武汉红喜 已提交
60
     * @param propertyResolver {@link PropertyResolver} Bean
武汉红喜's avatar
武汉红喜 已提交
61 62
     * @return {@link ServiceAnnotationBeanPostProcessor}
     */
武汉红喜's avatar
武汉红喜 已提交
63 64
    @ConditionalOnProperty(prefix = DUBBO_SCAN_PREFIX, name = BASE_PACKAGES_PROPERTY_NAME)
    @ConditionalOnBean(name = BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME)
武汉红喜's avatar
武汉红喜 已提交
65
    @Bean
武汉红喜's avatar
武汉红喜 已提交
66 67 68
    public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor(
            @Qualifier(BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME) PropertyResolver propertyResolver) {
        Set<String> packagesToScan = propertyResolver.getProperty(BASE_PACKAGES_PROPERTY_NAME, Set.class, emptySet());
武汉红喜's avatar
武汉红喜 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        return new ServiceAnnotationBeanPostProcessor(packagesToScan);
    }

    /**
     * Creates {@link ReferenceAnnotationBeanPostProcessor} Bean if Absent
     *
     * @return {@link ReferenceAnnotationBeanPostProcessor}
     */
    @ConditionalOnMissingBean
    @Bean(name = ReferenceAnnotationBeanPostProcessor.BEAN_NAME)
    public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor() {
        return new ReferenceAnnotationBeanPostProcessor();
    }

    /**
     * Single Dubbo Config Configuration
     *
     * @see EnableDubboConfig
     * @see DubboConfigConfiguration.Single
     */
武汉红喜's avatar
武汉红喜 已提交
89
    @Import(DubboConfigConfiguration.Single.class)
武汉红喜's avatar
武汉红喜 已提交
90 91 92 93 94 95 96 97 98
    protected static class SingleDubboConfigConfiguration {
    }

    /**
     * Multiple Dubbo Config Configuration , equals @EnableDubboConfig.multiple() == <code>true</code>
     *
     * @see EnableDubboConfig
     * @see DubboConfigConfiguration.Multiple
     */
武汉红喜's avatar
武汉红喜 已提交
99 100
    @ConditionalOnProperty(prefix = DUBBO_CONFIG_PREFIX, name = MULTIPLE_CONFIG_PROPERTY_NAME, matchIfMissing = true)
    @Import(DubboConfigConfiguration.Multiple.class)
武汉红喜's avatar
武汉红喜 已提交
101 102 103 104
    protected static class MultipleDubboConfigConfiguration {
    }

}