Skip to content

Commit

Permalink
use spring.factories instead of jdk spi (#1175)
Browse files Browse the repository at this point in the history
Co-authored-by: 致节 <hzj266771@antgroup.com>
  • Loading branch information
HzjNeverStop and 致节 authored May 24, 2023
1 parent a2c6b10 commit 50ee113
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.alipay.sofa.boot.autoconfigure.condition.ConditionalOnSwitch;
import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.boot.log.SofaBootLoggerFactory;
import com.alipay.sofa.boot.util.ServiceLoaderUtils;
import com.alipay.sofa.common.thread.NamedThreadFactory;
import com.alipay.sofa.common.thread.SofaThreadPoolExecutor;
import com.alipay.sofa.runtime.api.client.ReferenceClient;
Expand Down Expand Up @@ -57,8 +56,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.Assert;

import java.util.HashSet;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -116,17 +117,17 @@ public static SofaRuntimeContext sofaRuntimeContext(SofaRuntimeManager sofaRunti
@ConditionalOnMissingBean
public static BindingConverterFactory bindingConverterFactory() {
BindingConverterFactory bindingConverterFactory = new BindingConverterFactoryImpl();
bindingConverterFactory.addBindingConverters(ServiceLoaderUtils
.getClassesByServiceLoader(BindingConverter.class));
bindingConverterFactory.addBindingConverters(new HashSet<>(SpringFactoriesLoader
.loadFactories(BindingConverter.class, null)));
return bindingConverterFactory;
}

@Bean
@ConditionalOnMissingBean
public static BindingAdapterFactory bindingAdapterFactory() {
BindingAdapterFactory bindingAdapterFactory = new BindingAdapterFactoryImpl();
bindingAdapterFactory.addBindingAdapters(ServiceLoaderUtils
.getClassesByServiceLoader(BindingAdapter.class));
bindingAdapterFactory.addBindingAdapters(new HashSet<>(SpringFactoriesLoader.loadFactories(
BindingAdapter.class, null)));
return bindingAdapterFactory;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
org.springframework.context.ApplicationListener=\
com.alipay.sofa.rpc.boot.context.RpcEnvironmentApplicationListener
com.alipay.sofa.rpc.boot.context.RpcEnvironmentApplicationListener

com.alipay.sofa.boot.spring.namespace.spi.SofaBootTagNameSupport=\
com.alipay.sofa.rpc.boot.runtime.parser.GlobalFilterParser

com.alipay.sofa.runtime.spi.binding.BindingAdapter=\
com.alipay.sofa.rpc.boot.runtime.adapter.BoltBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.RestBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.DubboBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.H2cBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.HttpBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.TripleBindingAdapter

com.alipay.sofa.runtime.spi.service.BindingConverter=\
com.alipay.sofa.rpc.boot.runtime.converter.BoltBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.RestBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.DubboBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.H2cBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.HttpBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.TripleBindingConverter

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/
package com.alipay.sofa.rpc.boot.test.adapter;

import com.alipay.sofa.boot.util.ServiceLoaderUtils;
import com.alipay.sofa.runtime.service.impl.BindingAdapterFactoryImpl;
import com.alipay.sofa.runtime.spi.binding.BindingAdapter;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.HashSet;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -34,8 +36,8 @@ public class BindingAdapterFactoryTests {
@Test
public void checkOrder() {
BindingAdapterFactoryImpl bindingAdapterFactory = new BindingAdapterFactoryImpl();
bindingAdapterFactory.addBindingAdapters(ServiceLoaderUtils
.getClassesByServiceLoader(BindingAdapter.class));
bindingAdapterFactory.addBindingAdapters(new HashSet<>(SpringFactoriesLoader.loadFactories(
BindingAdapter.class, null)));
BindingAdapter bindingAdapter = bindingAdapterFactory.getBindingAdapter(XBindingAdapter.X);
assertThat(bindingAdapter).isInstanceOf(XBindingAdapter2.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alipay.sofa.rpc.boot.test.converter;

import com.alipay.sofa.boot.util.ServiceLoaderUtils;
import com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo;
import com.alipay.sofa.rpc.boot.runtime.converter.BoltBindingConverter;
import com.alipay.sofa.rpc.boot.runtime.converter.RpcBindingConverter;
Expand All @@ -27,7 +26,9 @@
import com.alipay.sofa.runtime.spi.service.BindingConverter;
import com.alipay.sofa.runtime.spi.service.BindingConverterFactory;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.HashSet;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -73,8 +74,8 @@ public void parseSofaMethods() {
@Test
public void checkOrder() {
BindingConverterFactory factory = new BindingConverterFactoryImpl();
factory.addBindingConverters(ServiceLoaderUtils
.getClassesByServiceLoader(BindingConverter.class));
factory.addBindingConverters(new HashSet<>(SpringFactoriesLoader.loadFactories(
BindingConverter.class, null)));
BindingConverter bindingConverter = factory.getBindingConverter(TestBindingConverter.TEST);
BindingConverter bindingConverterByTagName = factory
.getBindingConverterByTagName(TestBindingConverter.TARGET_NAME);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
com.alipay.sofa.boot.spring.namespace.spi.SofaBootTagNameSupport=\
com.alipay.sofa.rpc.boot.runtime.parser.GlobalFilterParser

com.alipay.sofa.runtime.spi.binding.BindingAdapter=\
com.alipay.sofa.rpc.boot.runtime.adapter.BoltBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.RestBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.DubboBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.H2cBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.HttpBindingAdapter,\
com.alipay.sofa.rpc.boot.runtime.adapter.TripleBindingAdapter,\
com.alipay.sofa.rpc.boot.test.adapter.XBindingAdapter,\
com.alipay.sofa.rpc.boot.test.adapter.XBindingAdapter2,\
com.alipay.sofa.rpc.boot.test.adapter.XBindingAdapter3

com.alipay.sofa.runtime.spi.service.BindingConverter=\
com.alipay.sofa.rpc.boot.runtime.converter.BoltBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.RestBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.DubboBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.H2cBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.HttpBindingConverter,\
com.alipay.sofa.rpc.boot.runtime.converter.TripleBindingConverter,\
com.alipay.sofa.rpc.boot.test.converter.TestBindingConverter,\
com.alipay.sofa.rpc.boot.test.converter.TestBindingConverter2,\
com.alipay.sofa.rpc.boot.test.converter.TestBindingConverter3

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Initializers
com.alipay.sofa.boot.startup.BeanStatCustomizer=\
com.alipay.sofa.runtime.startup.ComponentBeanStatCustomizer
com.alipay.sofa.runtime.startup.ComponentBeanStatCustomizer

com.alipay.sofa.boot.spring.namespace.spi.SofaBootTagNameSupport=\
com.alipay.sofa.runtime.spring.parser.ReferenceDefinitionParser,\
com.alipay.sofa.runtime.spring.parser.ServiceDefinitionParser,\
com.alipay.sofa.runtime.spring.parser.AsyncInitBeanDefinitionDecorator,\
com.alipay.sofa.runtime.ext.spring.parser.ExtensionPointBeanDefinitionParser,\
com.alipay.sofa.runtime.ext.spring.parser.ExtensionBeanDefinitionParser

com.alipay.sofa.runtime.spi.binding.BindingAdapter=\
com.alipay.sofa.runtime.service.binding.JvmBindingAdapter

com.alipay.sofa.runtime.spi.service.BindingConverter=\
com.alipay.sofa.runtime.service.binding.JvmBindingConverter
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alipay.sofa.runtime.spring;

import com.alipay.sofa.boot.util.ServiceLoaderUtils;
import com.alipay.sofa.runtime.api.client.ReferenceClient;
import com.alipay.sofa.runtime.api.client.ServiceClient;
import com.alipay.sofa.runtime.impl.ClientFactoryImpl;
Expand All @@ -34,6 +33,9 @@
import com.alipay.sofa.runtime.spi.service.BindingConverter;
import com.alipay.sofa.runtime.spi.service.BindingConverterFactory;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.HashSet;

/**
* @author huzijie
Expand All @@ -57,11 +59,11 @@ public abstract class SofaRuntimeManagerTestBase {
public void init() {
clientFactoryInternal = new ClientFactoryImpl();
bindingConverterFactory = new BindingConverterFactoryImpl();
bindingConverterFactory.addBindingConverters(ServiceLoaderUtils
.getClassesByServiceLoader(BindingConverter.class));
bindingConverterFactory.addBindingConverters(new HashSet<>(SpringFactoriesLoader
.loadFactories(BindingConverter.class, null)));
bindingAdapterFactory = new BindingAdapterFactoryImpl();
bindingAdapterFactory.addBindingAdapters(ServiceLoaderUtils
.getClassesByServiceLoader(BindingAdapter.class));
bindingAdapterFactory.addBindingAdapters(new HashSet<>(SpringFactoriesLoader.loadFactories(
BindingAdapter.class, null)));
sofaRuntimeManager = new StandardSofaRuntimeManager("testApp", Thread.currentThread()
.getContextClassLoader(), clientFactoryInternal);
clientFactoryInternal.registerClient(ReferenceClient.class, new ReferenceClientImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.ServiceLoader;
import java.util.List;

/**
* Implementation of {@link NamespaceHandlerSupport} to register {@link SofaBootTagNameSupport}.
Expand All @@ -39,8 +40,8 @@ public class SofaBootNamespaceHandler extends NamespaceHandlerSupport {

@Override
public void init() {
ServiceLoader<SofaBootTagNameSupport> serviceLoaderSofaBoot = ServiceLoader.load(SofaBootTagNameSupport.class);
serviceLoaderSofaBoot.forEach(this::registerTagParser);
List<SofaBootTagNameSupport> sofaBootTagNameSupports = SpringFactoriesLoader.loadFactories(SofaBootTagNameSupport.class, null);
sofaBootTagNameSupports.forEach(this::registerTagParser);
}

private void registerTagParser(SofaBootTagNameSupport tagNameSupport) {
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 50ee113

Please sign in to comment.