From 895d82b7bcb61007272d67daea760a90f59e83d8 Mon Sep 17 00:00:00 2001 From: yuanxuan Date: Tue, 28 Mar 2023 17:08:03 +0800 Subject: [PATCH 1/3] refactor aware processor --- .../runtime/SofaRuntimeAutoConfiguration.java | 14 +- ...ntFactoryAnnotationBeanPostProcessor.java} | 31 ++--- .../ExtensionClientBeanPostProcessor.java | 59 -------- .../spring/JvmFilterPostProcessor.java | 49 ------- ...untimeContextBeanFactoryPostProcessor.java | 22 ++- .../RuntimeShutdownAwarePostProcessor.java | 66 --------- .../spring/SofaRuntimeAwareProcessor.java | 84 +++++++++++ .../SofaRuntimeContextAwareProcessor.java | 58 -------- .../test/SofaRuntimeAwareProcessorTest.java | 131 ++++++++++++++++++ 9 files changed, 241 insertions(+), 273 deletions(-) rename sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/{ClientFactoryBeanPostProcessor.java => ClientFactoryAnnotationBeanPostProcessor.java} (85%) delete mode 100644 sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ExtensionClientBeanPostProcessor.java delete mode 100644 sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/JvmFilterPostProcessor.java delete mode 100644 sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeShutdownAwarePostProcessor.java create mode 100644 sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeAwareProcessor.java delete mode 100644 sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeContextAwareProcessor.java create mode 100644 sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java index 3b3893a2c..953070b49 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java @@ -36,9 +36,9 @@ import com.alipay.sofa.runtime.spi.service.BindingConverterFactory; import com.alipay.sofa.runtime.spring.AsyncInitBeanFactoryPostProcessor; import com.alipay.sofa.runtime.spring.AsyncProxyBeanPostProcessor; -import com.alipay.sofa.runtime.spring.JvmFilterPostProcessor; import com.alipay.sofa.runtime.spring.RuntimeContextBeanFactoryPostProcessor; import com.alipay.sofa.runtime.spring.ServiceBeanFactoryPostProcessor; +import com.alipay.sofa.runtime.spring.SofaRuntimeAwareProcessor; import com.alipay.sofa.runtime.spring.SofaShareBeanFactoryPostProcessor; import com.alipay.sofa.runtime.spring.async.AsyncTaskExecutionListener; import com.alipay.sofa.runtime.spring.share.SofaPostProcessorShareManager; @@ -130,12 +130,6 @@ public static RuntimeContextBeanFactoryPostProcessor runtimeContextBeanFactoryPo return new RuntimeContextBeanFactoryPostProcessor(); } - @Bean - @ConditionalOnMissingBean - public static JvmFilterPostProcessor jvmFilterPostProcessor() { - return new JvmFilterPostProcessor(); - } - @Bean @ConditionalOnMissingBean @ConditionalOnClass(name = "com.alipay.sofa.isle.ApplicationRuntimeModel") @@ -164,6 +158,12 @@ public static ProxyBeanFactoryPostProcessor proxyBeanFactoryPostProcessor() { return new ProxyBeanFactoryPostProcessor(); } + @Bean + @ConditionalOnMissingBean + public static SofaRuntimeAwareProcessor sofaRuntimeContextAwareProcessor(SofaRuntimeManager sofaRuntimeManager) { + return new SofaRuntimeAwareProcessor(sofaRuntimeManager); + } + public static Set getClassesByServiceLoader(Class clazz) { ServiceLoader serviceLoader = ServiceLoader.load(clazz); diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ClientFactoryBeanPostProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ClientFactoryAnnotationBeanPostProcessor.java similarity index 85% rename from sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ClientFactoryBeanPostProcessor.java rename to sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ClientFactoryAnnotationBeanPostProcessor.java index 25063aa9c..2d1fc9dbd 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ClientFactoryBeanPostProcessor.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ClientFactoryAnnotationBeanPostProcessor.java @@ -16,40 +16,37 @@ */ package com.alipay.sofa.runtime.spring; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - import com.alipay.sofa.boot.error.ErrorCode; +import com.alipay.sofa.runtime.api.annotation.SofaClientFactory; +import com.alipay.sofa.runtime.api.aware.ClientFactoryAware; +import com.alipay.sofa.runtime.api.client.ClientFactory; +import com.alipay.sofa.runtime.client.impl.ClientFactoryImpl; +import com.alipay.sofa.runtime.spring.singleton.SingletonSofaPostProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.PriorityOrdered; import org.springframework.util.ReflectionUtils; -import com.alipay.sofa.runtime.api.annotation.SofaClientFactory; -import com.alipay.sofa.runtime.api.aware.ClientFactoryAware; -import com.alipay.sofa.runtime.api.client.ClientFactory; -import com.alipay.sofa.runtime.client.impl.ClientFactoryImpl; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; /** - * {@link ClientFactoryAware} + * {@link ClientFactoryAware} * {@link SofaClientFactory} handler * * @author xuanbei 18/3/2 */ -public class ClientFactoryBeanPostProcessor implements BeanPostProcessor, PriorityOrdered { +@SingletonSofaPostProcessor +public class ClientFactoryAnnotationBeanPostProcessor implements BeanPostProcessor, PriorityOrdered { private ClientFactory clientFactory; - public ClientFactoryBeanPostProcessor(ClientFactory clientFactory) { + public ClientFactoryAnnotationBeanPostProcessor(ClientFactory clientFactory) { this.clientFactory = clientFactory; } @Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { - if (bean instanceof ClientFactoryAware) { - ((ClientFactoryAware) bean).setClientFactory(clientFactory); - } - ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() { @Override @@ -80,12 +77,6 @@ public boolean matches(Field field) { return bean; } - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - return bean; - } - @Override public int getOrder() { return PriorityOrdered.HIGHEST_PRECEDENCE; diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ExtensionClientBeanPostProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ExtensionClientBeanPostProcessor.java deleted file mode 100644 index 4a7dbe49d..000000000 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ExtensionClientBeanPostProcessor.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.core.PriorityOrdered; - -import com.alipay.sofa.runtime.api.aware.ExtensionClientAware; -import com.alipay.sofa.runtime.api.client.ExtensionClient; - -/** - * {@link ExtensionClientAware} - * - * @author ruoshan - * @since 2.6.0 - */ -public class ExtensionClientBeanPostProcessor implements BeanPostProcessor, PriorityOrdered { - - private ExtensionClient extensionClient; - - public ExtensionClientBeanPostProcessor(ExtensionClient extensionClient) { - this.extensionClient = extensionClient; - } - - @Override - public Object postProcessBeforeInitialization(final Object bean, String beanName) - throws BeansException { - if (bean instanceof ExtensionClientAware) { - ((ExtensionClientAware) bean).setExtensionClient(extensionClient); - } - return bean; - } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - return bean; - } - - @Override - public int getOrder() { - return PriorityOrdered.HIGHEST_PRECEDENCE; - } -} diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/JvmFilterPostProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/JvmFilterPostProcessor.java deleted file mode 100644 index d87c4d025..000000000 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/JvmFilterPostProcessor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.SofaRuntimeUtils; -import com.alipay.sofa.runtime.filter.JvmFilterHolder; -import com.alipay.sofa.runtime.filter.JvmFilter; -import com.alipay.sofa.runtime.spring.singleton.SingletonSofaPostProcessor; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.core.PriorityOrdered; - -/** - * @author Alaneuler - * Created on 2020/8/18 - */ -@SingletonSofaPostProcessor -public class JvmFilterPostProcessor implements BeanPostProcessor, PriorityOrdered { - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - if (!SofaRuntimeUtils.isArkEnvironment() || SofaRuntimeUtils.onMasterBiz()) { - if (bean instanceof JvmFilter) { - JvmFilterHolder.addJvmFilter((JvmFilter) bean); - } - } - - return bean; - } - - @Override - public int getOrder() { - return PriorityOrdered.HIGHEST_PRECEDENCE; - } -} diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeContextBeanFactoryPostProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeContextBeanFactoryPostProcessor.java index bc96a1eb0..93c694fd3 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeContextBeanFactoryPostProcessor.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeContextBeanFactoryPostProcessor.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.runtime.spring; +import com.alipay.sofa.runtime.spi.binding.BindingAdapterFactory; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; +import com.alipay.sofa.runtime.spi.service.BindingConverterFactory; +import com.alipay.sofa.runtime.spring.bean.SofaParameterNameDiscoverer; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -25,12 +29,6 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import com.alipay.sofa.runtime.ext.client.ExtensionClientImpl; -import com.alipay.sofa.runtime.spi.binding.BindingAdapterFactory; -import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; -import com.alipay.sofa.runtime.spi.service.BindingConverterFactory; -import com.alipay.sofa.runtime.spring.bean.SofaParameterNameDiscoverer; - /** * @author qilong.zql * @since 3.2.0 @@ -59,18 +57,14 @@ public RuntimeContextBeanFactoryPostProcessor(BindingAdapterFactory bindingAdapt public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // work on all bean - beanFactory.addBeanPostProcessor(new SofaRuntimeContextAwareProcessor(sofaRuntimeContext)); - beanFactory.addBeanPostProcessor(new ClientFactoryBeanPostProcessor(sofaRuntimeContext - .getClientFactory())); - beanFactory.addBeanPostProcessor(new ExtensionClientBeanPostProcessor( - new ExtensionClientImpl(sofaRuntimeContext))); + beanFactory.addBeanPostProcessor(new SofaRuntimeAwareProcessor(sofaRuntimeContext + .getSofaRuntimeManager())); + beanFactory.addBeanPostProcessor(new ClientFactoryAnnotationBeanPostProcessor( + sofaRuntimeContext.getClientFactory())); beanFactory .addBeanPostProcessor(new ReferenceAnnotationBeanPostProcessor(applicationContext, sofaRuntimeContext, bindingAdapterFactory, bindingConverterFactory)); - beanFactory.registerSingleton(RuntimeShutdownAwarePostProcessor.class.getSimpleName(), - new RuntimeShutdownAwarePostProcessor(sofaRuntimeContext.getSofaRuntimeManager())); - if (beanFactory instanceof AbstractAutowireCapableBeanFactory) { ((AbstractAutowireCapableBeanFactory) beanFactory) .setParameterNameDiscoverer(new SofaParameterNameDiscoverer(applicationContext diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeShutdownAwarePostProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeShutdownAwarePostProcessor.java deleted file mode 100644 index dd50d247e..000000000 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/RuntimeShutdownAwarePostProcessor.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 org.springframework.beans.BeansException; -import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; -import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.core.PriorityOrdered; - -import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager; -import com.alipay.sofa.runtime.spi.spring.RuntimeShutdownAware; - -/** - * @author xuanbei - * @author qilong.zql - * @since 2.5.0 - */ -public class RuntimeShutdownAwarePostProcessor implements MergedBeanDefinitionPostProcessor, - PriorityOrdered { - - private SofaRuntimeManager sofaRuntimeManager; - - public RuntimeShutdownAwarePostProcessor(SofaRuntimeManager sofaRuntimeManager) { - this.sofaRuntimeManager = sofaRuntimeManager; - } - - @Override - public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, - Class beanType, String beanName) { - // no process - } - - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { - return bean; - } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - if (bean instanceof RuntimeShutdownAware) { - sofaRuntimeManager.registerShutdownAware((RuntimeShutdownAware) bean); - } - return bean; - } - - @Override - public int getOrder() { - return PriorityOrdered.LOWEST_PRECEDENCE; - } -} diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeAwareProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeAwareProcessor.java new file mode 100644 index 000000000..8b204a743 --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeAwareProcessor.java @@ -0,0 +1,84 @@ +/* + * 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.SofaRuntimeUtils; +import com.alipay.sofa.runtime.api.aware.ClientFactoryAware; +import com.alipay.sofa.runtime.api.aware.ExtensionClientAware; +import com.alipay.sofa.runtime.ext.client.ExtensionClientImpl; +import com.alipay.sofa.runtime.filter.JvmFilter; +import com.alipay.sofa.runtime.filter.JvmFilterHolder; +import com.alipay.sofa.runtime.spi.client.ClientFactoryInternal; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager; +import com.alipay.sofa.runtime.spi.spring.RuntimeShutdownAware; +import com.alipay.sofa.runtime.spi.spring.SofaRuntimeContextAware; +import com.alipay.sofa.runtime.spring.singleton.SingletonSofaPostProcessor; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.core.PriorityOrdered; + +/** + * @author yuanxuan + * @version : SofaRuntimeAwareProcessor.java, v 0.1 2023年03月28日 15:52 yuanxuan Exp $ + */ +@SingletonSofaPostProcessor +public class SofaRuntimeAwareProcessor implements BeanPostProcessor, PriorityOrdered { + + private final SofaRuntimeManager sofaRuntimeManager; + + private final SofaRuntimeContext sofaRuntimeContext; + + private final ClientFactoryInternal clientFactory; + + private final ExtensionClientImpl extensionClient; + + public SofaRuntimeAwareProcessor(SofaRuntimeManager sofaRuntimeManager) { + this.sofaRuntimeManager = sofaRuntimeManager; + this.sofaRuntimeContext = sofaRuntimeManager.getSofaRuntimeContext(); + this.clientFactory = sofaRuntimeManager.getClientFactoryInternal(); + this.extensionClient = new ExtensionClientImpl(sofaRuntimeContext); + } + + @Override + public int getOrder() { + return PriorityOrdered.HIGHEST_PRECEDENCE; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) + throws BeansException { + if (bean instanceof SofaRuntimeContextAware) { + ((SofaRuntimeContextAware) bean).setSofaRuntimeContext(sofaRuntimeContext); + } + if (bean instanceof ClientFactoryAware) { + ((ClientFactoryAware) bean).setClientFactory(clientFactory); + } + if (bean instanceof ExtensionClientAware) { + ((ExtensionClientAware) bean).setExtensionClient(extensionClient); + } + if (!SofaRuntimeUtils.isArkEnvironment() || SofaRuntimeUtils.onMasterBiz()) { + if (bean instanceof JvmFilter) { + JvmFilterHolder.addJvmFilter((JvmFilter) bean); + } + } + if (bean instanceof RuntimeShutdownAware) { + sofaRuntimeManager.registerShutdownAware((RuntimeShutdownAware) bean); + } + return bean; + } +} diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeContextAwareProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeContextAwareProcessor.java deleted file mode 100644 index a0e928f22..000000000 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/SofaRuntimeContextAwareProcessor.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.core.PriorityOrdered; - -import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; -import com.alipay.sofa.runtime.spi.spring.SofaRuntimeContextAware; - -/** - * @author qilong.zql - * @author khotyn - * @since 2.5.0 - */ -public class SofaRuntimeContextAwareProcessor implements BeanPostProcessor, PriorityOrdered { - 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; - } - - @Override - public int getOrder() { - return PriorityOrdered.HIGHEST_PRECEDENCE; - } -} diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java new file mode 100644 index 000000000..fe87f363c --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java @@ -0,0 +1,131 @@ +/** + * Alipay.com Inc. + * Copyright (c) 2004-2023 All Rights Reserved. + */ +package com.alipay.sofa.runtime.test; + +import com.alipay.sofa.runtime.api.aware.ClientFactoryAware; +import com.alipay.sofa.runtime.api.aware.ExtensionClientAware; +import com.alipay.sofa.runtime.api.client.ClientFactory; +import com.alipay.sofa.runtime.api.client.ExtensionClient; +import com.alipay.sofa.runtime.filter.JvmFilter; +import com.alipay.sofa.runtime.filter.JvmFilterContext; +import com.alipay.sofa.runtime.filter.JvmFilterHolder; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext; +import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager; +import com.alipay.sofa.runtime.spi.spring.RuntimeShutdownAware; +import com.alipay.sofa.runtime.spi.spring.SofaRuntimeContextAware; +import com.alipay.sofa.runtime.spring.SofaRuntimeAwareProcessor; +import com.alipay.sofa.runtime.test.configuration.RuntimeConfiguration; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportResource; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author yuanxuan + * @version : SofaRuntimeAwareProcessorTest.java, v 0.1 2023年03月28日 16:23 yuanxuan Exp $ + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@TestPropertySource(properties = "spring.application.name=SofaRuntimeAwareProcessorTest") +public class SofaRuntimeAwareProcessorTest { + + @Autowired + private SofaRuntimeContext sofaRuntimeContext; + + @Test + public void testSofaRuntimeAware() { + SofaRuntimeAwareProcessor sofaRuntimeAwareProcessor = new SofaRuntimeAwareProcessor( + sofaRuntimeContext.getSofaRuntimeManager()); + SofaRuntimeManager sofaRuntimeManager = sofaRuntimeContext.getSofaRuntimeManager(); + SofaRuntimeAwareBean sofaRuntimeAwareBean = new SofaRuntimeAwareBean(); + sofaRuntimeAwareProcessor.postProcessBeforeInitialization(sofaRuntimeAwareBean, "testBean"); + + Assert.assertEquals(sofaRuntimeAwareBean.getSofaRuntimeContext(), + sofaRuntimeManager.getSofaRuntimeContext()); + Assert.assertEquals(sofaRuntimeAwareBean.getClientFactory(), + sofaRuntimeContext.getClientFactory()); + Assert.assertNotNull(sofaRuntimeAwareBean.getExtensionClient()); + Assert.assertTrue(JvmFilterHolder.getJvmFilters().contains(sofaRuntimeAwareBean)); + + Assert.assertFalse(sofaRuntimeAwareBean.isInvoked()); + sofaRuntimeManager.shutdown(); + Assert.assertTrue(sofaRuntimeAwareBean.isInvoked()); + } + + static class SofaRuntimeAwareBean implements SofaRuntimeContextAware, ClientFactoryAware, + ExtensionClientAware, JvmFilter, RuntimeShutdownAware { + + private ClientFactory clientFactory; + + private ExtensionClient extensionClient; + + private SofaRuntimeContext sofaRuntimeContext; + + private boolean invoked; + + @Override + public void setClientFactory(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public void setExtensionClient(ExtensionClient extensionClient) { + this.extensionClient = extensionClient; + } + + @Override + public boolean before(JvmFilterContext context) { + return false; + } + + @Override + public boolean after(JvmFilterContext context) { + return false; + } + + @Override + public void shutdown() { + invoked = true; + } + + @Override + public void setSofaRuntimeContext(SofaRuntimeContext sofaRuntimeContext) { + this.sofaRuntimeContext = sofaRuntimeContext; + } + + @Override + public int getOrder() { + return 0; + } + + public ClientFactory getClientFactory() { + return clientFactory; + } + + public ExtensionClient getExtensionClient() { + return extensionClient; + } + + public SofaRuntimeContext getSofaRuntimeContext() { + return sofaRuntimeContext; + } + + public boolean isInvoked() { + return invoked; + } + } + + @Configuration(proxyBeanMethods = false) + @Import(RuntimeConfiguration.class) + static class SofaRuntimeAwareProcessorTestConfiguration { + + } +} From da3a2de416fccb78c0f1e3b772e71d5669afd3d3 Mon Sep 17 00:00:00 2001 From: yuanxuan Date: Tue, 28 Mar 2023 17:42:29 +0800 Subject: [PATCH 2/3] remove duplicate define SofaRuntimeAwareProcessor --- .../autoconfigure/runtime/SofaRuntimeAutoConfiguration.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java index 953070b49..0d4f12c56 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java @@ -38,7 +38,6 @@ import com.alipay.sofa.runtime.spring.AsyncProxyBeanPostProcessor; import com.alipay.sofa.runtime.spring.RuntimeContextBeanFactoryPostProcessor; import com.alipay.sofa.runtime.spring.ServiceBeanFactoryPostProcessor; -import com.alipay.sofa.runtime.spring.SofaRuntimeAwareProcessor; import com.alipay.sofa.runtime.spring.SofaShareBeanFactoryPostProcessor; import com.alipay.sofa.runtime.spring.async.AsyncTaskExecutionListener; import com.alipay.sofa.runtime.spring.share.SofaPostProcessorShareManager; @@ -158,11 +157,6 @@ public static ProxyBeanFactoryPostProcessor proxyBeanFactoryPostProcessor() { return new ProxyBeanFactoryPostProcessor(); } - @Bean - @ConditionalOnMissingBean - public static SofaRuntimeAwareProcessor sofaRuntimeContextAwareProcessor(SofaRuntimeManager sofaRuntimeManager) { - return new SofaRuntimeAwareProcessor(sofaRuntimeManager); - } public static Set getClassesByServiceLoader(Class clazz) { ServiceLoader serviceLoader = ServiceLoader.load(clazz); From 26c7f3b05166469800897ec50c89fcf38f21fcba Mon Sep 17 00:00:00 2001 From: yuanxuan Date: Tue, 28 Mar 2023 17:54:37 +0800 Subject: [PATCH 3/3] fix pmd --- .../runtime/SofaRuntimeAutoConfiguration.java | 1 - .../test/SofaRuntimeAwareProcessorTest.java | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java index 0d4f12c56..a82edab81 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfiguration.java @@ -157,7 +157,6 @@ public static ProxyBeanFactoryPostProcessor proxyBeanFactoryPostProcessor() { return new ProxyBeanFactoryPostProcessor(); } - public static Set getClassesByServiceLoader(Class clazz) { ServiceLoader serviceLoader = ServiceLoader.load(clazz); diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java index fe87f363c..c576dd8f2 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaRuntimeAwareProcessorTest.java @@ -1,6 +1,18 @@ -/** - * Alipay.com Inc. - * Copyright (c) 2004-2023 All Rights Reserved. +/* + * 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.test; @@ -24,7 +36,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.ImportResource; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner;