spring @Order標記


@Order標記定義了組件的加載順序。

@Order標記從spring 2.0出現,但是在spring 4.0之前,@Order標記只支持AspectJ的切面排序。spring 4.0對@Order做了增強,它開始支持對裝載在諸如Lists和Arrays容器中的自動包裝(auto-wired)組件的排序。

在spring內部,對基於spring xml的應用,spring使用OrderComparator類來實現排序。對基於注解的應用,spring采用AnnotationAwareOrderComparator來實現排序。

@Order 標記定義如下:

@Retention(value=RUNTIME) @Target(value={TYPE,METHOD,FIELD}) @Documented public @interface Order

這個標記包含一個value屬性。屬性接受整形值。如:1,2 等等。值越小擁有越高的優先級。
下面讓我們來看一個使用spring 3.x 和spring 4.x 的例子:

Ranks.java

package com.javapapers.spring3.autowire.collection; public interface Ranks { }

RankOne.java

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

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

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

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

<?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

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); } }

輸出結果:
[RankOne, RankThree, RankTwo]


從結果可以看出,結果是沒有順序的。因為spring 3.x不支持對自動包裝組件的排序。

接下來,我升級spring的版本至4.x, 並對RanOne,RankTwo和RankThree加上order標記,值做相應的調整。
@Component
@Order(1)
public class RankOne implements Ranks{
private String rank = "RankOne";
    
    public String toString(){
        return this.rank;
    }
}

輸出結果如下:
[RankOne, RankTwo, RankThree]
參考文章: http://javapapers.com/spring/spring-order-annotation/ 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM