Skip to content

Remove redundant @Order and add corresponding ArchRule #44917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ static List<ArchRule> standard() {
rules.add(classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
rules.add(methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
rules.add(conditionsShouldNotBePublic());
rules.add(classLevelProhibitRedundantOrderAnnotation());
rules.add(methodLevelProhibitRedundantOrderAnnotation());
return List.copyOf(rules);
}

Expand Down Expand Up @@ -306,6 +308,35 @@ private static ArchRule conditionsShouldNotBePublic() {
.allowEmptyShould(true);
}

private static ArchRule classLevelProhibitRedundantOrderAnnotation() {
return ArchRuleDefinition.classes()
.that()
.areAnnotatedWith("org.springframework.core.annotation.Order")
.should(prohibitRedundantOrderAnnotation())
.allowEmptyShould(true);
}

private static ArchRule methodLevelProhibitRedundantOrderAnnotation() {
return ArchRuleDefinition.methods()
.that()
.areAnnotatedWith("org.springframework.core.annotation.Order")
.should(prohibitRedundantOrderAnnotation())
.allowEmptyShould(true);
}

private static ArchCondition<? super HasAnnotations<?>> prohibitRedundantOrderAnnotation() {
return check("prohibit redundant @Order(Ordered.LOWEST_PRECEDENCE) or empty @Order",
(HasAnnotations<?> item, ConditionEvents events) -> {
JavaAnnotation<?> orderAnnotation = item
.getAnnotationOfType("org.springframework.core.annotation.Order");
int value = (int) orderAnnotation.getProperties().get("value");
if (value == Integer.MAX_VALUE) {
addViolation(events, item,
orderAnnotation.getDescription() + " should be removed since it's redundant");
}
});
}

private static boolean containsOnlySingleType(JavaType[] types, JavaType type) {
return types.length == 1 && type.equals(types[0]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,8 +18,6 @@

import org.springframework.boot.actuate.health.HealthEndpointGroups;
import org.springframework.boot.actuate.health.HealthEndpointGroupsPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

/**
Expand All @@ -29,7 +27,6 @@
* @author Phillip Webb
* @author Madhura Bhave
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class AvailabilityProbesHealthEndpointGroupsPostProcessor implements HealthEndpointGroupsPostProcessor {

private final boolean addAdditionalPaths;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,7 +47,6 @@
* @see ManagementContextConfiguration
* @see ImportCandidates
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware {

private ClassLoader classLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,8 +41,6 @@
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -122,7 +120,6 @@ private Properties createCacheManagerProperties(
* as defined per {@link JCacheProviderAvailableCondition} or if a
* {@link CacheManager} has already been defined.
*/
@Order(Ordered.LOWEST_PRECEDENCE)
static class JCacheAvailableCondition extends AnyNestedCondition {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotation.Adapt;
import org.springframework.core.annotation.MergedAnnotationCollectors;
import org.springframework.core.annotation.MergedAnnotationPredicates;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.util.Assert;
Expand All @@ -81,7 +79,6 @@
* @see ConditionalOnMissingBean
* @see ConditionalOnSingleCandidate
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class OnBeanCondition extends FilteringSpringBootCondition implements ConfigurationCondition {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,8 +34,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Scope;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClient.Builder;

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

@Bean
@ConditionalOnMissingBean
@Order(Ordered.LOWEST_PRECEDENCE)
HttpMessageConvertersRestClientCustomizer httpMessageConvertersRestClientCustomizer(
ObjectProvider<HttpMessageConverters> messageConverters) {
return new HttpMessageConvertersRestClientCustomizer(messageConverters.getIfUnique());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,8 +38,6 @@
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

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

@Bean
@ConditionalOnNotWarDeployment
@Order(Ordered.LOWEST_PRECEDENCE)
@ConditionalOnMissingBean(name = "websocketUpgradeFilterWebServerCustomizer")
WebServerFactoryCustomizer<JettyServletWebServerFactory> websocketUpgradeFilterWebServerCustomizer() {
return (factory) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.task.VirtualThreadTaskExecutor;
import org.springframework.retry.RetryPolicy;
Expand Down Expand Up @@ -1187,7 +1186,6 @@ MessageRecoverer anotherMessageRecoverer() {
static class MultipleRabbitTemplateCustomizersConfiguration {

@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
RabbitTemplateCustomizer secondCustomizer() {
return mock(RabbitTemplateCustomizer.class);
}
Expand Down Expand Up @@ -1356,7 +1354,6 @@ ConnectionFactoryCustomizer connectionFactoryCustomizer() {
static class MultipleConnectionFactoryCustomizersConfiguration {

@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
ConnectionFactoryCustomizer secondCustomizer() {
return mock(ConnectionFactoryCustomizer.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,6 @@ static class CustomExceptionHandler extends ResponseEntityExceptionHandler {
static class OrderedControllerAdviceBeansConfiguration {

@ControllerAdvice
@Order
static class LowestOrderedControllerAdvice {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,6 @@ CustomExceptionHandler customExceptionHandler() {
static class OrderedControllerAdviceBeansConfiguration {

@ControllerAdvice
@Order
static class LowestOrderedControllerAdvice {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -304,7 +304,6 @@ static class CustomHighWebSocketMessageBrokerConfigurer implements WebSocketMess
}

@Component
@Order(Ordered.LOWEST_PRECEDENCE)
static class CustomLowWebSocketMessageBrokerConfigurer implements WebSocketMessageBrokerConfigurer {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,8 +31,6 @@
import org.springframework.boot.devtools.system.DevToolsEnablementDeducer;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.NativeDetector;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
Expand All @@ -48,7 +46,6 @@
* @author Madhura Bhave
* @since 1.3.0
*/
@Order(Ordered.LOWEST_PRECEDENCE)
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {

private static final Log logger = DevToolsLogFactory.getLog(DevToolsPropertyDefaultsPostProcessor.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Role;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
Expand Down Expand Up @@ -100,7 +98,6 @@ public DataSource dataSource(Environment environment) {
return new EmbeddedDataSourceFactory(environment).getEmbeddedDatabase();
}

@Order(Ordered.LOWEST_PRECEDENCE)
static class EmbeddedDataSourceBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {

private static final ConfigurationPropertyName DATASOURCE_URL_PROPERTY = ConfigurationPropertyName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,11 +42,9 @@
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationFilter;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.Order;
import org.springframework.core.style.ToStringCreator;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.test.context.ContextCustomizer;
Expand Down Expand Up @@ -176,7 +174,6 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
* {@link BeanDefinitionRegistryPostProcessor} to cleanup temporary configuration
* added to load imports.
*/
@Order(Ordered.LOWEST_PRECEDENCE)
static class ImportsCleanupPostProcessor implements BeanDefinitionRegistryPostProcessor {

static final String BEAN_NAME = ImportsCleanupPostProcessor.class.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,8 +24,6 @@
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
* {@link BeanFactoryPostProcessor} to prevent {@link AutoCloseable} destruction calls so
Expand All @@ -36,7 +34,6 @@
* @author Stephane Nicoll
* @see TestcontainersLifecycleApplicationContextInitializer
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class TestcontainersLifecycleBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,8 +41,6 @@
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.ApplicationListener;
import org.springframework.context.aot.AbstractAotProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.log.LogMessage;

/**
Expand All @@ -60,7 +58,6 @@
* @see TestcontainersLifecycleApplicationContextInitializer
*/
@SuppressWarnings({ "removal", "deprecation" })
@Order(Ordered.LOWEST_PRECEDENCE)
class TestcontainersLifecycleBeanPostProcessor
implements DestructionAwareBeanPostProcessor, ApplicationListener<BeforeTestcontainerUsedEvent> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,6 @@

import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.boot.diagnostics.FailureAnalyzer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;

/**
Expand All @@ -32,7 +30,6 @@
*
* @author Phillip Webb
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class MissingParameterNamesFailureAnalyzer implements FailureAnalyzer {

private static final String USE_PARAMETERS_MESSAGE = "Ensure that the compiler uses the '-parameters' flag";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,6 @@ static class BeanDefinitionOrderRunnerConfig {
private final List<String> runners = new ArrayList<>();

@Bean
@Order
CommandLineRunner runnerC() {
return (args) -> this.runners.add("runnerC");
}
Expand Down
Loading