From fcd365c416dd572187dc12598a65b3ea207c83ea Mon Sep 17 00:00:00 2001 From: HzjNeverStop <441627022@qq.com> Date: Thu, 25 Jan 2024 15:19:26 +0800 Subject: [PATCH] Update to 3.22.0 (#1282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support ExcludeBeanNameAutoProxyCreator (#1277) Co-authored-by: 致节 (cherry picked from commit f708c0cf757c769c932982f22c17b43f37155aeb) * 1. skip low cost spring.context.config-classes.enhance and bean-factory #1270 2. Support ExcludeBeanNameAutoProxyCreator #1277 3. avoid ReadinessCheckListener cuncrrency * relase 3.22.0 --------- Co-authored-by: 致节 --- pom.xml | 2 +- .../healthcheck/ReadinessCheckListener.java | 5 +- .../alipay/sofa/startup/StartupReporter.java | 12 ++- .../ExcludeBeanNameAutoProxyCreator.java | 74 +++++++++++++ .../ExcludeBeanNameAutoProxyCreatorTest.java | 102 ++++++++++++++++++ 5 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreator.java create mode 100644 sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreatorTest.java diff --git a/pom.xml b/pom.xml index 86d4c1dd8..1eae329ec 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,7 @@ SOFABoot Build - 3.21.0 + 3.22.0 ${revision} 1.6.7 diff --git a/sofa-boot-project/sofa-boot-core/healthcheck-sofa-boot/src/main/java/com/alipay/sofa/healthcheck/ReadinessCheckListener.java b/sofa-boot-project/sofa-boot-core/healthcheck-sofa-boot/src/main/java/com/alipay/sofa/healthcheck/ReadinessCheckListener.java index 8b5c86a14..8efd79618 100644 --- a/sofa-boot-project/sofa-boot-core/healthcheck-sofa-boot/src/main/java/com/alipay/sofa/healthcheck/ReadinessCheckListener.java +++ b/sofa-boot-project/sofa-boot-core/healthcheck-sofa-boot/src/main/java/com/alipay/sofa/healthcheck/ReadinessCheckListener.java @@ -40,6 +40,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @@ -71,11 +72,11 @@ public class ReadinessCheckListener implements ApplicationContextAware, Ordered, private boolean healthCheckerStatus = true; - private Map healthCheckerDetails = new HashMap<>(); + private Map healthCheckerDetails = new ConcurrentHashMap<>(); private boolean healthIndicatorStatus = true; - private Map healthIndicatorDetails = new HashMap<>(); + private Map healthIndicatorDetails = new ConcurrentHashMap<>(); private boolean healthCallbackStatus = true; private boolean readinessCheckFinish = false; diff --git a/sofa-boot-project/sofa-boot-core/startup-sofa-boot/src/main/java/com/alipay/sofa/startup/StartupReporter.java b/sofa-boot-project/sofa-boot-core/startup-sofa-boot/src/main/java/com/alipay/sofa/startup/StartupReporter.java index 6b8fcee80..357115845 100644 --- a/sofa-boot-project/sofa-boot-core/startup-sofa-boot/src/main/java/com/alipay/sofa/startup/StartupReporter.java +++ b/sofa-boot-project/sofa-boot-core/startup-sofa-boot/src/main/java/com/alipay/sofa/startup/StartupReporter.java @@ -52,15 +52,23 @@ public class StartupReporter { public static final String SPRING_CONTEXT_BEAN_FACTORY_POST_PROCESSOR = "spring.context.bean-factory.post-process"; + public static final String SPRING_BEAN_POST_PROCESSOR = "spring.context.beans.post-process"; + + public static final String SPRING_CONFIG_CLASSES_ENHANCE = "spring.context.config-classes.enhance"; + public static final Collection SPRING_BEAN_INSTANTIATE_TYPES = new HashSet<>(); public static final Collection SPRING_CONTEXT_POST_PROCESSOR_TYPES = new HashSet<>(); + public static final Collection SPRING_CONFIG_CLASSES_ENHANCE_TYPES = new HashSet<>(); + static { SPRING_BEAN_INSTANTIATE_TYPES.add(SPRING_BEANS_INSTANTIATE); SPRING_BEAN_INSTANTIATE_TYPES.add(SPRING_BEANS_SMART_INSTANTIATE); SPRING_CONTEXT_POST_PROCESSOR_TYPES.add(SPRING_CONTEXT_BEANDEF_REGISTRY_POST_PROCESSOR); SPRING_CONTEXT_POST_PROCESSOR_TYPES.add(SPRING_CONTEXT_BEAN_FACTORY_POST_PROCESSOR); + SPRING_CONFIG_CLASSES_ENHANCE_TYPES.add(SPRING_CONFIG_CLASSES_ENHANCE); + SPRING_CONFIG_CLASSES_ENHANCE_TYPES.add(SPRING_BEAN_POST_PROCESSOR); } private final StartupStaticsModel startupStaticsModel = new StartupStaticsModel(); @@ -175,7 +183,9 @@ public List generateBeanStats(ConfigurableApplicationContext context) private boolean filterBeanInitializeByCost(BeanStat beanStat) { String name = beanStat.getBeanType(); - if (SPRING_BEAN_INSTANTIATE_TYPES.contains(name)) { + if (SPRING_BEAN_INSTANTIATE_TYPES.contains(name) + || SPRING_CONTEXT_POST_PROCESSOR_TYPES.contains(name) + || SPRING_CONFIG_CLASSES_ENHANCE_TYPES.contains(name)) { return beanStat.getCost() >= beanInitCostThreshold; } else { return true; diff --git a/sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreator.java b/sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreator.java new file mode 100644 index 000000000..7c2ee76a2 --- /dev/null +++ b/sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreator.java @@ -0,0 +1,74 @@ +/* + * 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.boot.aop.framework.autoproxy; + +import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; +import org.springframework.util.PatternMatchUtils; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * Extension for {@link BeanNameAutoProxyCreator} to support exclude specify bean names. + * + * @author huzijie + * @version ExcludeBeanNameAutoProxyCreator.java, v 0.1 2024年01月04日 4:24 PM huzijie Exp $ + */ +public class ExcludeBeanNameAutoProxyCreator extends BeanNameAutoProxyCreator { + + @Nullable + private List excludeBeanNames; + + /** + * Set the names of the beans that should not automatically get wrapped with proxies. + * A name can specify a prefix to match by ending with "*", e.g. "myBean,tx*" + * will match the bean named "myBean" and all beans whose name start with "tx". + *

NOTE: In case of a FactoryBean, only the objects created by the + * FactoryBean will get proxied. This default behavior applies as of Spring 2.0. + * If you intend to proxy a FactoryBean instance itself (a rare use case, but + * Spring 1.2's default behavior), specify the bean name of the FactoryBean + * including the factory-bean prefix "&": e.g. "&myFactoryBean". + * @see org.springframework.beans.factory.FactoryBean + * @see org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX + */ + public void setExcludeBeanNames(String... beanNames) { + Assert.notEmpty(beanNames, "'excludeBeanNames' must not be empty"); + this.excludeBeanNames = new ArrayList<>(beanNames.length); + for (String mappedName : beanNames) { + this.excludeBeanNames.add(StringUtils.trimWhitespace(mappedName)); + } + } + + @Override + protected boolean isMatch(String beanName, String mappedName) { + return super.isMatch(beanName, mappedName) && !isExcluded(beanName); + } + + private boolean isExcluded(String beanName) { + if (excludeBeanNames != null) { + for (String mappedName : this.excludeBeanNames) { + if (PatternMatchUtils.simpleMatch(mappedName, beanName)) { + return true; + } + } + } + return false; + } +} diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreatorTest.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreatorTest.java new file mode 100644 index 000000000..083e0aa20 --- /dev/null +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/aop/framework/autoproxy/ExcludeBeanNameAutoProxyCreatorTest.java @@ -0,0 +1,102 @@ +/* + * 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.boot.aop.framework.autoproxy; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ExcludeBeanNameAutoProxyCreator}. + * + * @author huzijie + * @version ExcludeBeanNameAutoProxyCreatorTests.java, v 0.1 2024年01月04日 4:36 PM huzijie Exp $ + */ +public class ExcludeBeanNameAutoProxyCreatorTest { + + @Test + public void excludeBeanNames() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( + ExcludeBeanNameAutoProxyCreatorTestConfiguration.class); + SampleInterface sampleA = context.getBean("sampleA", SampleInterface.class); + SampleInterface sampleB = context.getBean("sampleBeanB", SampleInterface.class); + SampleInterface sampleC = context.getBean("sampleBeanC", SampleInterface.class); + assertThat(sampleA.hello()).isEqualTo("hello"); + assertThat(sampleB.hello()).isEqualTo("aop"); + assertThat(sampleC.hello()).isEqualTo("hello"); + } + + @Configuration + static class ExcludeBeanNameAutoProxyCreatorTestConfiguration { + + @Bean + public SampleInterface sampleA() { + return new SampleInterfaceImpl(); + } + + @Bean + public SampleInterface sampleBeanB() { + return new SampleInterfaceImpl(); + } + + @Bean + public SampleInterface sampleBeanC() { + return new SampleInterfaceImpl(); + } + + @Bean + public ExcludeBeanNameAutoProxyCreator excludeBeanNameAutoProxyCreator() { + ExcludeBeanNameAutoProxyCreator autoProxyCreator = new ExcludeBeanNameAutoProxyCreator(); + autoProxyCreator.setBeanNames("sampleBean*"); + autoProxyCreator.setExcludeBeanNames("sampleBeanC"); + autoProxyCreator.setInterceptorNames("sampleAdvisor"); + return autoProxyCreator; + } + + @Bean + public MethodInterceptor sampleAdvisor() { + return new MethodInterceptor() { + @Nullable + @Override + public Object invoke(@Nonnull MethodInvocation invocation) { + return "aop"; + } + }; + } + } + + interface SampleInterface { + + String hello(); + } + + static class SampleInterfaceImpl implements SampleInterface { + + @Override + public String hello() { + return "hello"; + } + } +}