Skip to content

Commit 0079cb3

Browse files
committed
Remove redundant @order and add corresponding ArchRule
Signed-off-by: Yanming Zhou <[email protected]>
1 parent c5eb394 commit 0079cb3

File tree

21 files changed

+43
-58
lines changed

21 files changed

+43
-58
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static List<ArchRule> standard() {
9696
rules.add(classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
9797
rules.add(methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
9898
rules.add(conditionsShouldNotBePublic());
99+
rules.add(classLevelProhibitRedundantOrderAnnotation());
100+
rules.add(methodLevelProhibitRedundantOrderAnnotation());
99101
return List.copyOf(rules);
100102
}
101103

@@ -306,6 +308,35 @@ private static ArchRule conditionsShouldNotBePublic() {
306308
.allowEmptyShould(true);
307309
}
308310

311+
private static ArchRule classLevelProhibitRedundantOrderAnnotation() {
312+
return ArchRuleDefinition.classes()
313+
.that()
314+
.areAnnotatedWith("org.springframework.core.annotation.Order")
315+
.should(prohibitRedundantOrderAnnotation())
316+
.allowEmptyShould(true);
317+
}
318+
319+
private static ArchRule methodLevelProhibitRedundantOrderAnnotation() {
320+
return ArchRuleDefinition.methods()
321+
.that()
322+
.areAnnotatedWith("org.springframework.core.annotation.Order")
323+
.should(prohibitRedundantOrderAnnotation())
324+
.allowEmptyShould(true);
325+
}
326+
327+
private static ArchCondition<? super HasAnnotations<?>> prohibitRedundantOrderAnnotation() {
328+
return check("prohibit redundant @Order(Ordered.LOWEST_PRECEDENCE) or empty @Order",
329+
(HasAnnotations<?> item, ConditionEvents events) -> {
330+
JavaAnnotation<?> orderAnnotation = item
331+
.getAnnotationOfType("org.springframework.core.annotation.Order");
332+
int value = (int) orderAnnotation.getProperties().get("value");
333+
if (value == Integer.MAX_VALUE) {
334+
addViolation(events, item,
335+
orderAnnotation.getDescription() + " should be removed since it's redundant");
336+
}
337+
});
338+
}
339+
309340
private static boolean containsOnlySingleType(JavaType[] types, JavaType type) {
310341
return types.length == 1 && type.equals(types[0]);
311342
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/availability/AvailabilityProbesHealthEndpointGroupsPostProcessor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@
1818

1919
import org.springframework.boot.actuate.health.HealthEndpointGroups;
2020
import org.springframework.boot.actuate.health.HealthEndpointGroupsPostProcessor;
21-
import org.springframework.core.Ordered;
22-
import org.springframework.core.annotation.Order;
2321
import org.springframework.core.env.Environment;
2422

2523
/**
@@ -29,7 +27,6 @@
2927
* @author Phillip Webb
3028
* @author Madhura Bhave
3129
*/
32-
@Order(Ordered.LOWEST_PRECEDENCE)
3330
class AvailabilityProbesHealthEndpointGroupsPostProcessor implements HealthEndpointGroupsPostProcessor {
3431

3532
private final boolean addAdditionalPaths;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelector.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,6 @@
4747
* @see ManagementContextConfiguration
4848
* @see ImportCandidates
4949
*/
50-
@Order(Ordered.LOWEST_PRECEDENCE)
5150
class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware {
5251

5352
private ClassLoader classLoader;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,6 @@
4141
import org.springframework.context.annotation.Conditional;
4242
import org.springframework.context.annotation.Configuration;
4343
import org.springframework.context.annotation.Import;
44-
import org.springframework.core.Ordered;
45-
import org.springframework.core.annotation.Order;
4644
import org.springframework.core.io.Resource;
4745
import org.springframework.core.type.AnnotatedTypeMetadata;
4846
import org.springframework.util.CollectionUtils;
@@ -122,7 +120,6 @@ private Properties createCacheManagerProperties(
122120
* as defined per {@link JCacheProviderAvailableCondition} or if a
123121
* {@link CacheManager} has already been defined.
124122
*/
125-
@Order(Ordered.LOWEST_PRECEDENCE)
126123
static class JCacheAvailableCondition extends AnyNestedCondition {
127124

128125
JCacheAvailableCondition() {
@@ -146,7 +143,6 @@ static class CustomJCacheCacheManager {
146143
* {@link CachingProvider} has been found or if the property referring to the provider
147144
* to use has been set.
148145
*/
149-
@Order(Ordered.LOWEST_PRECEDENCE)
150146
static class JCacheProviderAvailableCondition extends SpringBootCondition {
151147

152148
@Override

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@
5050
import org.springframework.context.annotation.Condition;
5151
import org.springframework.context.annotation.ConditionContext;
5252
import org.springframework.context.annotation.ConfigurationCondition;
53-
import org.springframework.core.Ordered;
5453
import org.springframework.core.ResolvableType;
5554
import org.springframework.core.annotation.MergedAnnotation;
5655
import org.springframework.core.annotation.MergedAnnotation.Adapt;
5756
import org.springframework.core.annotation.MergedAnnotationCollectors;
5857
import org.springframework.core.annotation.MergedAnnotationPredicates;
5958
import org.springframework.core.annotation.MergedAnnotations;
60-
import org.springframework.core.annotation.Order;
6159
import org.springframework.core.type.AnnotatedTypeMetadata;
6260
import org.springframework.core.type.MethodMetadata;
6361
import org.springframework.util.Assert;
@@ -81,7 +79,6 @@
8179
* @see ConditionalOnMissingBean
8280
* @see ConditionalOnSingleCandidate
8381
*/
84-
@Order(Ordered.LOWEST_PRECEDENCE)
8582
class OnBeanCondition extends FilteringSpringBootCondition implements ConfigurationCondition {
8683

8784
@Override

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,8 +34,6 @@
3434
import org.springframework.context.annotation.Bean;
3535
import org.springframework.context.annotation.Conditional;
3636
import org.springframework.context.annotation.Scope;
37-
import org.springframework.core.Ordered;
38-
import org.springframework.core.annotation.Order;
3937
import org.springframework.web.client.RestClient;
4038
import org.springframework.web.client.RestClient.Builder;
4139

@@ -58,7 +56,6 @@ public class RestClientAutoConfiguration {
5856

5957
@Bean
6058
@ConditionalOnMissingBean
61-
@Order(Ordered.LOWEST_PRECEDENCE)
6259
HttpMessageConvertersRestClientCustomizer httpMessageConvertersRestClientCustomizer(
6360
ObjectProvider<HttpMessageConverters> messageConverters) {
6461
return new HttpMessageConvertersRestClientCustomizer(messageConverters.getIfUnique());

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketServletAutoConfiguration.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,8 +38,6 @@
3838
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
3939
import org.springframework.context.annotation.Bean;
4040
import org.springframework.context.annotation.Configuration;
41-
import org.springframework.core.Ordered;
42-
import org.springframework.core.annotation.Order;
4341

4442
/**
4543
* Auto configuration for WebSocket servlet server in embedded Tomcat, Jetty or Undertow.
@@ -91,7 +89,6 @@ JettyWebSocketServletWebServerCustomizer websocketServletWebServerCustomizer() {
9189

9290
@Bean
9391
@ConditionalOnNotWarDeployment
94-
@Order(Ordered.LOWEST_PRECEDENCE)
9592
@ConditionalOnMissingBean(name = "websocketUpgradeFilterWebServerCustomizer")
9693
WebServerFactoryCustomizer<JettyServletWebServerFactory> websocketUpgradeFilterWebServerCustomizer() {
9794
return (factory) -> {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import org.springframework.context.annotation.Configuration;
8080
import org.springframework.context.annotation.Import;
8181
import org.springframework.context.annotation.Primary;
82-
import org.springframework.core.Ordered;
8382
import org.springframework.core.annotation.Order;
8483
import org.springframework.core.task.VirtualThreadTaskExecutor;
8584
import org.springframework.retry.RetryPolicy;
@@ -1187,7 +1186,6 @@ MessageRecoverer anotherMessageRecoverer() {
11871186
static class MultipleRabbitTemplateCustomizersConfiguration {
11881187

11891188
@Bean
1190-
@Order(Ordered.LOWEST_PRECEDENCE)
11911189
RabbitTemplateCustomizer secondCustomizer() {
11921190
return mock(RabbitTemplateCustomizer.class);
11931191
}
@@ -1356,7 +1354,6 @@ ConnectionFactoryCustomizer connectionFactoryCustomizer() {
13561354
static class MultipleConnectionFactoryCustomizersConfiguration {
13571355

13581356
@Bean
1359-
@Order(Ordered.LOWEST_PRECEDENCE)
13601357
ConnectionFactoryCustomizer secondCustomizer() {
13611358
return mock(ConnectionFactoryCustomizer.class);
13621359
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,6 @@ static class CustomExceptionHandler extends ResponseEntityExceptionHandler {
11261126
static class OrderedControllerAdviceBeansConfiguration {
11271127

11281128
@ControllerAdvice
1129-
@Order
11301129
static class LowestOrderedControllerAdvice {
11311130

11321131
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,6 @@ CustomExceptionHandler customExceptionHandler() {
15661566
static class OrderedControllerAdviceBeansConfiguration {
15671567

15681568
@ControllerAdvice
1569-
@Order
15701569
static class LowestOrderedControllerAdvice {
15711570

15721571
}

0 commit comments

Comments
 (0)