一、前言
通过实现springframework的spring-core源码包中org.springframework.core.annotation.Order加载排序接口,从而可以实现对于在SpringContext上下文bean对象加载初始化顺序,可以通过简单调整排序号实现类复杂业务逻辑优化调整调配等。
二、代码示例
1. 配置项目spring环境(可以下载参见springhelloworld完整项目及依赖包)
2. 模拟实现三个不同加载顺序类:Order1>Order2>Order3(从大到小依赖加载:ORDERED分别设置为:1、2、3)
package com.xwood.springmvc.test.order;@b@@b@import org.springframework.core.annotation.Order;@b@import org.springframework.stereotype.Component;@b@@b@@Component @b@@b@//使用order属性,设置该类在spring容器中的加载顺序@b@@Order(1) @b@public class Order1 {@b@ @b@ private final int ORDERED = 1;@b@ @b@ public Order1(){@b@ System.out.println(this);@b@ }@b@ @b@ @Override@b@ public String toString() {@b@ return "Order1 is loaded @ORDERED=" + ORDERED + "]";@b@ }@b@@b@}
package com.xwood.springmvc.test.order;@b@@b@import org.springframework.core.annotation.Order;@b@import org.springframework.stereotype.Component;@b@@b@@Component @b@@b@//使用order属性,设置该类在spring容器中的加载顺序@b@@Order(2) @b@public class Order2 {@b@ @b@ private final int ORDERED = 2;@b@ @b@ public Order2(){@b@ System.out.println(this);@b@ }@b@ @b@ @Override@b@ public String toString() {@b@ return "Order2 is loaded @ORDERED=" + ORDERED + "]";@b@ }@b@@b@}
package com.xwood.springmvc.test.order;@b@@b@import org.springframework.core.annotation.Order;@b@import org.springframework.stereotype.Component;@b@@b@@Component @b@@b@//使用order属性,设置该类在spring容器中的加载顺序@b@@Order(2) @b@public class Order3 {@b@ @b@ private final int ORDERED = 3;@b@ @b@ public Order3(){@b@ System.out.println(this);@b@ }@b@ @b@ @Override@b@ public String toString() {@b@ return "Order3 is loaded @ORDERED=" + ORDERED + "]";@b@ }@b@@b@}
3. 运行junit测试类
package com.xwood.springmvc.test;@b@@b@import org.junit.Test;@b@import org.springframework.test.context.ContextConfiguration;@b@import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;@b@@b@@b@@ContextConfiguration(locations={"classpath*:junitSpringContext.xml"})@b@public class TestBase extends AbstractJUnit4SpringContextTests { @b@@b@ @Test@b@ public void testBean() {@b@ }@b@@b@}
junitSpringContext.xml
<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @b@ xmlns:mvc="http://www.springframework.org/schema/mvc" @b@ xmlns:aop="http://www.springframework.org/schema/aop" @b@ xmlns:context="http://www.springframework.org/schema/context"@b@ xsi:schemaLocation="http://www.springframework.org/schema/beans@b@ http://www.springframework.org/schema/beans/spring-beans-4.1.xsd@b@ http://www.springframework.org/schema/context@b@ http://www.springframework.org/schema/context/spring-context-4.1.xsd">@b@ @b@ <context:annotation-config /> @b@ <context:component-scan base-package="com"></context:component-scan>@b@ @b@</beans>
控制台打印结果
十二月 16, 2018 8:48:50 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners@b@...@b@信息: Refreshing org.springframework.context.support.GenericApplicationContext@3d2b3: startup date [Sun Dec 16 20:48:51 CST 2018]; root of context hierarchy@b@@b@Order1 is loaded @ORDERED=1]@b@Order1 is loaded @ORDERED=2]@b@Order1 is loaded @ORDERED=3]@b@@b@十二月 16, 2018 8:48:52 下午 org.springframework.context.support.GenericApplicationContext doClose@b@信息: Closing org.springframework.context.support.GenericApplicationContext@3d2b3: startup date [Sun Dec 16 20:48:51 CST 2018]; root of context hierarchy