SpringBoot開(kāi)機(jī)啟動(dòng)與@Order注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.example.zcw.runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @Classname BootApplicationRunner * @Description TODO * @Date 2020/3/6 13:06 * @Created by zhaocunwei */ @Order ( 2 ) @Slf4j @Component public class BootApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { log.info( "This is BootApplicationRunner ..." ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.example.zcw.runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @Classname BootCommandLineRunner * @Description TODO * @Date 2020/3/6 13:09 * @Created by zhaocunwei */ @Order ( 1 ) @Slf4j @Component public class BootCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { log.info( "This is BootCommandLineRunner ..." ); } } |
spring @Order標(biāo)記
@Order標(biāo)記定義了組件的加載順序
@Order標(biāo)記從spring 2.0出現(xiàn),但是在spring 4.0之前,@Order標(biāo)記只支持AspectJ的切面排序。spring 4.0對(duì)@Order做了增強(qiáng),它開(kāi)始支持對(duì)裝載在諸如Lists和Arrays容器中的自動(dòng)包裝(auto-wired)組件的排序。
在spring內(nèi)部,對(duì)基于spring xml的應(yīng)用,spring使用OrderComparator類來(lái)實(shí)現(xiàn)排序。對(duì)基于注解的應(yīng)用,spring采用AnnotationAwareOrderComparator來(lái)實(shí)現(xiàn)排序。
@Order 標(biāo)記定義如下:
1
2
3
4
|
@Retention (value=RUNTIME) @Target (value={TYPE,METHOD,FIELD}) @Documented public @interface Order |
這個(gè)標(biāo)記包含一個(gè)value屬性。屬性接受整形值。如:1,2 等等。值越小擁有越高的優(yōu)先級(jí)。
下面讓我們來(lái)看一個(gè)
使用spring 3.x 和spring 4.x 的例子
Ranks.java
1
2
3
|
package com.javapapers.spring3.autowire.collection; public interface Ranks { } |
RankOne.java
1
2
3
4
5
6
7
8
9
10
11
|
package com.javapapers.spring3.autowire.collection; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component public class RankOne implements Ranks{ private String rank = "RankOne" ; public String toString(){ return this .rank; } } |
RankTwo.java
1
2
3
4
5
6
7
8
9
10
11
|
package com.javapapers.spring3.autowire.collection; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component public class RankTwo implements Ranks{ private String rank = "RankTwo" ; public String toString(){ return this .rank; } } |
RankThree.java
1
2
3
4
5
6
7
8
9
10
11
|
package com.javapapers.spring3.autowire.collection; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component public class RankThree implements Ranks{ private String rank = "RankThree" ; public String toString(){ return this .rank; } } |
Results.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Component to hold student ranks in a collection as shown below. package com.javapapers.spring3.autowire.collection; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Results { @Autowired private List ranks ; @Override public String toString(){ return ranks.toString(); } } |
beans.xml
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> < context:annotation-config /> < context:component-scan base-package = "com.javapapers.spring3" /> </ beans > |
RanksClient.java
1
2
3
4
5
6
7
8
9
10
|
package com.javapapers.spring3.autowire.collection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class RanksClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" ); Results results = (Results)context.getBean( "results" ); System.out.println(results); } } |
輸出結(jié)果:
[RankOne, RankThree, RankTwo]
從結(jié)果可以看出,結(jié)果是沒(méi)有順序的。因?yàn)閟pring 3.x不支持對(duì)自動(dòng)包裝組件的排序。
接下來(lái),我升級(jí)spring的版本至4.x, 并對(duì)RanOne,RankTwo和RankThree加上order標(biāo)記,值做相應(yīng)的調(diào)整。
1
2
3
4
5
6
7
8
|
@Component @Order ( 1 ) public class RankOne implements Ranks{ private String rank = "RankOne" ; public String toString(){ return this .rank; } } |
輸出結(jié)果如下:
[RankOne, RankTwo, RankThree]
參考文章: http://javapapers.com/spring/spring-order-annotation/
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://zhaocunwei.blog.csdn.net/article/details/104694000