org.springframework.core.Ordered接口

本文探讨了Ordered接口的用途及实现方式,解释了如何通过优先级设置影响类的加载顺序,以PropertyPlaceholderConfigurer为例进行深入解读。

关于Ordered接口,用过的人可能知道,这里我谈下自己的理解。也希望各位大神能给予指点。

源码如下:

/**
* Interface that can be implemented by objects that should be
* orderable, for example in a Collection.
*
* <p>The actual order can be interpreted as prioritization, with
* the first object (with the lowest order value) having the highest
* priority.
*
* <p>Note that there is a 'priority' marker for this interface:
* {@link PriorityOrdered}. Order values expressed by PriorityOrdered
* objects always apply before order values of 'plain' Ordered values.
*
* @author Juergen Hoeller
* @since 07.04.2003
* @see OrderComparator
* @see org.springframework.core.annotation.Order
*/

public interface Ordered {


/**
* Useful constant for the highest precedence value.
* @see java.lang.Integer#MIN_VALUE
*/
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;


/**
* Useful constant for the lowest precedence value.
* @see java.lang.Integer#MAX_VALUE
*/
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;




/**
* Return the order value of this object, with a
* higher value meaning greater in terms of sorting.
* <p>Normally starting with 0, with <code>Integer.MAX_VALUE</code>
* indicating the greatest value. Same order values will result
* in arbitrary positions for the affected objects.
* <p>Higher values can be interpreted as lower priority. As a
* consequence, the object with the lowest value has highest priority
* (somewhat analogous to Servlet "load-on-startup" values).
* @return the order value
*/
int getOrder();


}

当然这里并不是无故提出这个接口来看的。事由是由PropertyPlaceholderConfigurer这个类的加载顺序导致的。

这个类是实现了Ordered接口并且有这样一句话

private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered

来说明PropertyPlaceholderConfigurer加载的顺序,如果你细心的话,Ordered上面的一个注释你会看到。

大概的翻译就是值越小加载优先级越高,而普通的对象我们定义的是没有实现Ordered接口的,即是他所说的

'plain' Ordered values.这样的对象的加载优先级比实现Ordered接口优先级最低的还要低。就是说虽然

PropertyPlaceholderConfigurer类的优先级是实现Ordered接口中最低的,但也比plain Object的优先级高。所以我们在

定义PropertyPlaceholderConfigurer的时候,可以把位置放后面而先用其值。如图:




Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@323ebcc6] 2025-08-28 10:06:27.938 DEBUG 24804 --- [ main] o.s.a.aspectj.AspectJExpressionPointcut : Could not access current invocation - matching with limited context: java.lang.IllegalStateException: No MethodInvocation found: Check that an AOP invocation is in progress and that the ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, note that advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor! In addition, ExposeInvocationInterceptor and ExposeInvocationInterceptor.currentInvocation() must be invoked from the same thread. 2025-08-28 10:06:27.943 DEBUG 24804 --- [ main] c.d.datasource.aspect.DataSourceAspect : Set datasource to: tdengine 2025-08-28 10:06:27.947 DEBUG 24804 --- [ main] c.d.datasource.aspect.DataSourceAspect : Restored datasource to: default 2025-08-28 10:06:27.948 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.948 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 6, missCount = 1] 2025-08-28 10:06:27.950 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.951 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 7, missCount = 1] 2025-08-28 10:06:27.951 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.951 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 8, missCount = 1] 2025-08-28 10:06:27.952 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.952 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 9, missCount = 1] 2025-08-28 10:06:27.955 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.955 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 10, missCount = 1] 2025-08-28 10:06:27.957 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.957 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 11, missCount = 1] 2025-08-28 10:06:27.958 DEBUG 24804 --- [ main] tractDirtiesContextTestExecutionListener : After test method: context [DefaultTestContext@4758820d testClass = SyncBmsDataTestServicess, testInstance = com.depower.SyncBmsDataTestServicess@6362dcf2, testMethod = syncDataTest@SyncBmsDataTestServicess, testException = java.lang.NullPointerException, mergedContextConfiguration = [WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null]. 2025-08-28 10:06:27.958 DEBUG 24804 --- [ main] o.s.t.c.w.ServletTestExecutionListener : Resetting RequestContextHolder for test context [DefaultTestContext@4758820d testClass = SyncBmsDataTestServicess, testInstance = com.depower.SyncBmsDataTestServicess@6362dcf2, testMethod = syncDataTest@SyncBmsDataTestServicess, testException = java.lang.NullPointerException, mergedContextConfiguration = [WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]. 2025-08-28 10:06:27.965 DEBUG 24804 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext [8996952] from cache with key [[WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] 2025-08-28 10:06:27.965 DEBUG 24804 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache@41058886 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 12, missCount = 1] 2025-08-28 10:06:27.965 DEBUG 24804 --- [ main] tractDirtiesContextTestExecutionListener : After test class: context [DefaultTestContext@4758820d testClass = SyncBmsDataTestServicess, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@74eb909f testClass = SyncBmsDataTestServicess, locations = '{}', classes = '{class com.depower.DepowerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@313b2ea6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@623a8092, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@157853da, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@306cf3ea], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.support.DependencyInjectionTestExecutionListener.reinjectDependencies' -> true]], class annotated with @DirtiesContext [false] with mode [null]. 2025-08-28 10:06:27.972 DEBUG 24804 --- [extShutdownHook] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 2147483647 2025-08-28 10:06:27.972 DEBUG 24804 --- [extShutdownHook] o.s.c.support.DefaultLifecycleProcessor : Bean 'documentationPluginsBootstrapper' completed its stop procedure 2025-08-28 10:06:27.974 INFO 24804 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler' 2025-08-28 10:06:27.975 DEBUG 24804 --- [extShutdownHook] o.s.d.r.l.RedisMessageListenerContainer : Stopped RedisMessageListenerContainer 2025-08-28 10:06:27.975 INFO 24804 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2025-08-28 10:06:27.980 INFO 24804 --- [extShutdownHook] o.s.s.quartz.SchedulerFactoryBean : Shutting down Quartz Scheduler 2025-08-28 10:06:27.980 INFO 24804 --- [extShutdownHook] org.quartz.core.QuartzScheduler : Scheduler DepowerScheduler_$_D213791756346770213 shutting down. 2025-08-28 10:06:27.980 INFO 24804 --- [extShutdownHook] org.quartz.core.QuartzScheduler : Scheduler DepowerScheduler_$_D213791756346770213 paused. 2025-08-28 10:06:27.981 DEBUG 24804 --- [extShutdownHook] org.quartz.simpl.SimpleThreadPool : Shutting down threadpool... 2025-08-28 10:06:27.981 DEBUG 24804 --- [extShutdownHook] org.quartz.simpl.SimpleThreadPool : Shutdown of threadpool complete. 2025-08-28 10:06:27.981 DEBUG 24804 --- [extShutdownHook] o.s.s.quartz.LocalDataSourceJobStore : JobStore background threads shutdown. 2025-08-28 10:06:27.981 INFO 24804 --- [extShutdownHook] org.quartz.core.QuartzScheduler : Scheduler DepowerScheduler_$_D213791756346770213 shutdown complete. 2025-08-28 10:06:27.987 DEBUG 24804 --- [extShutdownHook] io.lettuce.core.RedisClient : Initiate shutdown (100, 100, MILLISECONDS) 2025-08-28 10:06:27.995 DEBUG 24804 --- [extShutdownHook] i.l.c.resource.DefaultClientResources : Initiate shutdown (0, 2, SECONDS) 2025-08-28 10:06:27.996 DEBUG 24804 --- [extShutdownHook] i.l.c.r.DefaultEventLoopGroupProvider : Initiate shutdown (0, 2, SECONDS) 2025-08-28 10:06:28.005 DEBUG 24804 --- [extShutdownHook] o.a.s.spring.LifecycleBeanPostProcessor : Destroying bean [securityManager]... 2025-08-28 10:06:28.015 DEBUG 24804 --- [eduler_Worker-5] org.quartz.simpl.SimpleThreadPool : WorkerThread is shut down. 2025-08-28 10:06:28.015 DEBUG 24804 --- [eduler_Worker-3] org.quartz.simpl.SimpleThreadPool : WorkerThread is shut down. 2025-08-28 10:06:28.015 DEBUG 24804 --- [eduler_Worker-7] org.quartz.simpl.SimpleThreadPool : WorkerThread is shut down.
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值