Skip to content

Commit fcd365c

Browse files
HzjNeverStop致节
andauthored
Update to 3.22.0 (#1282)
* support ExcludeBeanNameAutoProxyCreator (#1277) Co-authored-by: 致节 <[email protected]> (cherry picked from commit f708c0c) * 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: 致节 <[email protected]>
1 parent 9505851 commit fcd365c

File tree

5 files changed

+191
-4
lines changed

5 files changed

+191
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<description>SOFABoot Build</description>
3838

3939
<properties>
40-
<revision>3.21.0</revision>
40+
<revision>3.22.0</revision>
4141
<sofa.boot.version>${revision}</sofa.boot.version>
4242
<!--maven plugin-->
4343
<maven.staging.plugin>1.6.7</maven.staging.plugin>

sofa-boot-project/sofa-boot-core/healthcheck-sofa-boot/src/main/java/com/alipay/sofa/healthcheck/ReadinessCheckListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import java.util.HashMap;
4242
import java.util.Map;
43+
import java.util.concurrent.ConcurrentHashMap;
4344
import java.util.concurrent.atomic.AtomicBoolean;
4445
import java.util.stream.Collectors;
4546

@@ -71,11 +72,11 @@ public class ReadinessCheckListener implements ApplicationContextAware, Ordered,
7172

7273
private boolean healthCheckerStatus = true;
7374

74-
private Map<String, Health> healthCheckerDetails = new HashMap<>();
75+
private Map<String, Health> healthCheckerDetails = new ConcurrentHashMap<>();
7576

7677
private boolean healthIndicatorStatus = true;
7778

78-
private Map<String, Health> healthIndicatorDetails = new HashMap<>();
79+
private Map<String, Health> healthIndicatorDetails = new ConcurrentHashMap<>();
7980

8081
private boolean healthCallbackStatus = true;
8182
private boolean readinessCheckFinish = false;

sofa-boot-project/sofa-boot-core/startup-sofa-boot/src/main/java/com/alipay/sofa/startup/StartupReporter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,23 @@ public class StartupReporter {
5252

5353
public static final String SPRING_CONTEXT_BEAN_FACTORY_POST_PROCESSOR = "spring.context.bean-factory.post-process";
5454

55+
public static final String SPRING_BEAN_POST_PROCESSOR = "spring.context.beans.post-process";
56+
57+
public static final String SPRING_CONFIG_CLASSES_ENHANCE = "spring.context.config-classes.enhance";
58+
5559
public static final Collection<String> SPRING_BEAN_INSTANTIATE_TYPES = new HashSet<>();
5660

5761
public static final Collection<String> SPRING_CONTEXT_POST_PROCESSOR_TYPES = new HashSet<>();
5862

63+
public static final Collection<String> SPRING_CONFIG_CLASSES_ENHANCE_TYPES = new HashSet<>();
64+
5965
static {
6066
SPRING_BEAN_INSTANTIATE_TYPES.add(SPRING_BEANS_INSTANTIATE);
6167
SPRING_BEAN_INSTANTIATE_TYPES.add(SPRING_BEANS_SMART_INSTANTIATE);
6268
SPRING_CONTEXT_POST_PROCESSOR_TYPES.add(SPRING_CONTEXT_BEANDEF_REGISTRY_POST_PROCESSOR);
6369
SPRING_CONTEXT_POST_PROCESSOR_TYPES.add(SPRING_CONTEXT_BEAN_FACTORY_POST_PROCESSOR);
70+
SPRING_CONFIG_CLASSES_ENHANCE_TYPES.add(SPRING_CONFIG_CLASSES_ENHANCE);
71+
SPRING_CONFIG_CLASSES_ENHANCE_TYPES.add(SPRING_BEAN_POST_PROCESSOR);
6472
}
6573

6674
private final StartupStaticsModel startupStaticsModel = new StartupStaticsModel();
@@ -175,7 +183,9 @@ public List<BeanStat> generateBeanStats(ConfigurableApplicationContext context)
175183

176184
private boolean filterBeanInitializeByCost(BeanStat beanStat) {
177185
String name = beanStat.getBeanType();
178-
if (SPRING_BEAN_INSTANTIATE_TYPES.contains(name)) {
186+
if (SPRING_BEAN_INSTANTIATE_TYPES.contains(name)
187+
|| SPRING_CONTEXT_POST_PROCESSOR_TYPES.contains(name)
188+
|| SPRING_CONFIG_CLASSES_ENHANCE_TYPES.contains(name)) {
179189
return beanStat.getCost() >= beanInitCostThreshold;
180190
} else {
181191
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.boot.aop.framework.autoproxy;
18+
19+
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
20+
import org.springframework.lang.Nullable;
21+
import org.springframework.util.Assert;
22+
import org.springframework.util.PatternMatchUtils;
23+
import org.springframework.util.StringUtils;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
/**
29+
* Extension for {@link BeanNameAutoProxyCreator} to support exclude specify bean names.
30+
*
31+
* @author huzijie
32+
* @version ExcludeBeanNameAutoProxyCreator.java, v 0.1 2024年01月04日 4:24 PM huzijie Exp $
33+
*/
34+
public class ExcludeBeanNameAutoProxyCreator extends BeanNameAutoProxyCreator {
35+
36+
@Nullable
37+
private List<String> excludeBeanNames;
38+
39+
/**
40+
* Set the names of the beans that should not automatically get wrapped with proxies.
41+
* A name can specify a prefix to match by ending with "*", e.g. "myBean,tx*"
42+
* will match the bean named "myBean" and all beans whose name start with "tx".
43+
* <p><b>NOTE:</b> In case of a FactoryBean, only the objects created by the
44+
* FactoryBean will get proxied. This default behavior applies as of Spring 2.0.
45+
* If you intend to proxy a FactoryBean instance itself (a rare use case, but
46+
* Spring 1.2's default behavior), specify the bean name of the FactoryBean
47+
* including the factory-bean prefix "&amp;": e.g. "&amp;myFactoryBean".
48+
* @see org.springframework.beans.factory.FactoryBean
49+
* @see org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX
50+
*/
51+
public void setExcludeBeanNames(String... beanNames) {
52+
Assert.notEmpty(beanNames, "'excludeBeanNames' must not be empty");
53+
this.excludeBeanNames = new ArrayList<>(beanNames.length);
54+
for (String mappedName : beanNames) {
55+
this.excludeBeanNames.add(StringUtils.trimWhitespace(mappedName));
56+
}
57+
}
58+
59+
@Override
60+
protected boolean isMatch(String beanName, String mappedName) {
61+
return super.isMatch(beanName, mappedName) && !isExcluded(beanName);
62+
}
63+
64+
private boolean isExcluded(String beanName) {
65+
if (excludeBeanNames != null) {
66+
for (String mappedName : this.excludeBeanNames) {
67+
if (PatternMatchUtils.simpleMatch(mappedName, beanName)) {
68+
return true;
69+
}
70+
}
71+
}
72+
return false;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.boot.aop.framework.autoproxy;
18+
19+
import org.aopalliance.intercept.MethodInterceptor;
20+
import org.aopalliance.intercept.MethodInvocation;
21+
import org.junit.Test;
22+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
import javax.annotation.Nonnull;
27+
import javax.annotation.Nullable;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link ExcludeBeanNameAutoProxyCreator}.
33+
*
34+
* @author huzijie
35+
* @version ExcludeBeanNameAutoProxyCreatorTests.java, v 0.1 2024年01月04日 4:36 PM huzijie Exp $
36+
*/
37+
public class ExcludeBeanNameAutoProxyCreatorTest {
38+
39+
@Test
40+
public void excludeBeanNames() {
41+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
42+
ExcludeBeanNameAutoProxyCreatorTestConfiguration.class);
43+
SampleInterface sampleA = context.getBean("sampleA", SampleInterface.class);
44+
SampleInterface sampleB = context.getBean("sampleBeanB", SampleInterface.class);
45+
SampleInterface sampleC = context.getBean("sampleBeanC", SampleInterface.class);
46+
assertThat(sampleA.hello()).isEqualTo("hello");
47+
assertThat(sampleB.hello()).isEqualTo("aop");
48+
assertThat(sampleC.hello()).isEqualTo("hello");
49+
}
50+
51+
@Configuration
52+
static class ExcludeBeanNameAutoProxyCreatorTestConfiguration {
53+
54+
@Bean
55+
public SampleInterface sampleA() {
56+
return new SampleInterfaceImpl();
57+
}
58+
59+
@Bean
60+
public SampleInterface sampleBeanB() {
61+
return new SampleInterfaceImpl();
62+
}
63+
64+
@Bean
65+
public SampleInterface sampleBeanC() {
66+
return new SampleInterfaceImpl();
67+
}
68+
69+
@Bean
70+
public ExcludeBeanNameAutoProxyCreator excludeBeanNameAutoProxyCreator() {
71+
ExcludeBeanNameAutoProxyCreator autoProxyCreator = new ExcludeBeanNameAutoProxyCreator();
72+
autoProxyCreator.setBeanNames("sampleBean*");
73+
autoProxyCreator.setExcludeBeanNames("sampleBeanC");
74+
autoProxyCreator.setInterceptorNames("sampleAdvisor");
75+
return autoProxyCreator;
76+
}
77+
78+
@Bean
79+
public MethodInterceptor sampleAdvisor() {
80+
return new MethodInterceptor() {
81+
@Nullable
82+
@Override
83+
public Object invoke(@Nonnull MethodInvocation invocation) {
84+
return "aop";
85+
}
86+
};
87+
}
88+
}
89+
90+
interface SampleInterface {
91+
92+
String hello();
93+
}
94+
95+
static class SampleInterfaceImpl implements SampleInterface {
96+
97+
@Override
98+
public String hello() {
99+
return "hello";
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)