Skip to content

Commit

Permalink
Optimize auto-configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongZhang authored and caojie09 committed Sep 20, 2018
1 parent c6c05e7 commit 8765bad
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
@Configuration
public class SofaModuleAutoConfiguration {
@Bean
public SofaModuleProperties sofaModuleProperties() {
return new SofaModuleProperties();
public static SofaModuleBeanFactoryPostProcessor sofaModuleBeanFactoryPostProcessor() {
return new SofaModuleBeanFactoryPostProcessor();
}

@Bean
public SofaModuleBeanFactoryPostProcessor sofaModuleBeanFactoryPostProcessor() {
return new SofaModuleBeanFactoryPostProcessor();
public SofaModuleProperties sofaModuleProperties() {
return new SofaModuleProperties();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 com.alipay.sofa.runtime.spi.spring;

import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;

/**
* Interface to be implemented by any object that wishes to be notified
* of the {@link com.alipay.sofa.runtime.spi.component.SofaRuntimeContext} that it runs in.
*
* @author qilong.zql
* @author khotyn
* @since 2.5.0
*/
public interface SofaRuntimeContextAware {
void setSofaRuntimeContext(SofaRuntimeContext sofaRuntimeContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 com.alipay.sofa.runtime.spring;

import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import com.alipay.sofa.runtime.spi.spring.SofaRuntimeContextAware;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
* @author qilong.zql
* @author khotyn
* @since 2.5.0
*/
public class SofaRuntimeContextAwareProcessor implements BeanPostProcessor {
private SofaRuntimeContext sofaRuntimeContext;

public SofaRuntimeContextAwareProcessor(SofaRuntimeContext sofaRuntimeContext) {
this.sofaRuntimeContext = sofaRuntimeContext;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof SofaRuntimeContextAware) {
((SofaRuntimeContextAware) bean).setSofaRuntimeContext(sofaRuntimeContext);
}

return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import com.alipay.sofa.runtime.spring.ApplicationShutdownCallbackPostProcessor;
import com.alipay.sofa.runtime.spring.ClientFactoryBeanPostProcessor;
import com.alipay.sofa.runtime.spring.ServiceAnnotationBeanPostProcessor;
import com.alipay.sofa.runtime.spring.SofaRuntimeContextAwareProcessor;
import com.alipay.sofa.runtime.spring.callback.CloseApplicationContextCallBack;
import com.alipay.sofa.runtime.spring.config.SofaRuntimeConfigurationProperties;
import com.alipay.sofa.runtime.spring.health.DefaultRuntimeHealthChecker;
import com.alipay.sofa.runtime.spring.health.MultiApplicationHealthIndicator;
import com.alipay.sofa.runtime.spring.health.SofaComponentHealthChecker;
import com.alipay.sofa.runtime.spring.health.SofaComponentHealthIndicator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -63,26 +65,29 @@
@EnableConfigurationProperties(SofaRuntimeConfigurationProperties.class)
public class SofaRuntimeAutoConfiguration {
@Bean
public BindingConverterFactory bindingConverterFactory() {
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static BindingConverterFactory bindingConverterFactory() {
BindingConverterFactory bindingConverterFactory = new BindingConverterFactoryImpl();
bindingConverterFactory
.addBindingConverters(getClassesByServiceLoader(BindingConverter.class));
return bindingConverterFactory;
}

@Bean
public BindingAdapterFactory bindingAdapterFactory() {
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static BindingAdapterFactory bindingAdapterFactory() {
BindingAdapterFactory bindingAdapterFactory = new BindingAdapterFactoryImpl();
bindingAdapterFactory.addBindingAdapters(getClassesByServiceLoader(BindingAdapter.class));
return bindingAdapterFactory;
}

@Bean
public SofaRuntimeContext sofaRuntimeContext(@Value("${"
+ CommonMiddlewareConstants.APP_NAME_KEY
+ "}") String appName,
BindingConverterFactory bindingConverterFactory,
BindingAdapterFactory bindingAdapterFactory) {
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static SofaRuntimeContext sofaRuntimeContext(@Value("${"
+ CommonMiddlewareConstants.APP_NAME_KEY
+ "}") String appName,
BindingConverterFactory bindingConverterFactory,
BindingAdapterFactory bindingAdapterFactory) {
ClientFactoryInternal clientFactoryInternal = new ClientFactoryImpl();
SofaRuntimeManager sofaRuntimeManager = new StandardSofaRuntimeManager(appName,
SofaRuntimeAutoConfiguration.class.getClassLoader(), clientFactoryInternal);
Expand All @@ -100,23 +105,28 @@ public SofaRuntimeContext sofaRuntimeContext(@Value("${"
}

@Bean
public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor(BindingAdapterFactory bindingAdapterFactory,
BindingConverterFactory bindingConverterFactory) {
public static ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor(BindingAdapterFactory bindingAdapterFactory,
BindingConverterFactory bindingConverterFactory) {
return new ServiceAnnotationBeanPostProcessor(bindingAdapterFactory,
bindingConverterFactory);
}

@Bean
public ClientFactoryBeanPostProcessor clientFactoryBeanPostProcessor(SofaRuntimeContext sofaRuntimeContext) {
public static ClientFactoryBeanPostProcessor clientFactoryBeanPostProcessor(SofaRuntimeContext sofaRuntimeContext) {
return new ClientFactoryBeanPostProcessor(sofaRuntimeContext.getClientFactory());
}

@Bean
public ApplicationShutdownCallbackPostProcessor applicationShutdownCallbackPostProcessor(SofaRuntimeContext sofaRuntimeContext) {
public static ApplicationShutdownCallbackPostProcessor applicationShutdownCallbackPostProcessor(SofaRuntimeContext sofaRuntimeContext) {
return new ApplicationShutdownCallbackPostProcessor(
sofaRuntimeContext.getSofaRuntimeManager());
}

@Bean
public static SofaRuntimeContextAwareProcessor sofaRuntimeContextAwareProcessor(SofaRuntimeContext sofaRuntimeContext) {
return new SofaRuntimeContextAwareProcessor(sofaRuntimeContext);
}

@Bean
public CloseApplicationContextCallBack closeApplicationContextCallBack() {
return new CloseApplicationContextCallBack();
Expand Down

0 comments on commit 8765bad

Please sign in to comment.