亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于SpringBoot開(kāi)機(jī)啟動(dòng)與@Order注解

 更新時(shí)間:2021年09月15日 10:48:06   作者:總是幸福的老豌豆  
這篇文章主要介紹了SpringBoot開(kāi)機(jī)啟動(dòng)與@Order注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot開(kāi)機(jī)啟動(dòng)與@Order注解

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 ...");
    }
}
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)來(lái)實(shí)現(xiàn)排序。對(duì)基于注解的應(yīng)用,spring采用AnnotationAwareOrderComparator來(lái)實(shí)現(xiàn)排序。

@Order 標(biāo)記定義如下:

@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

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

輸出結(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)整。

@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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Bean中的六種作用域你了解嗎

    Spring?Bean中的六種作用域你了解嗎

    Bean的作用域是指Bean實(shí)例的生命周期及可見(jiàn)性范圍,Spring框架定義了6種作用域,本文就來(lái)和大家聊聊這6種作用域的定義與使用,希望對(duì)大家有所幫助
    2023-09-09
  • 為什么Java開(kāi)發(fā)需要配置環(huán)境變量

    為什么Java開(kāi)發(fā)需要配置環(huán)境變量

    這篇文章主要介紹了為什么Java開(kāi)發(fā)需要配置環(huán)境變量,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-08-08
  • Java及nginx實(shí)現(xiàn)文件權(quán)限控制代碼實(shí)例

    Java及nginx實(shí)現(xiàn)文件權(quán)限控制代碼實(shí)例

    這篇文章主要介紹了Java及nginx實(shí)現(xiàn)文件權(quán)限控制代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解)

    使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring

    這篇文章主要介紹了使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解),需要的朋友可以參考下
    2018-01-01
  • SpringMVC+MyBatis實(shí)現(xiàn)多數(shù)據(jù)源切換

    SpringMVC+MyBatis實(shí)現(xiàn)多數(shù)據(jù)源切換

    在企業(yè)級(jí)應(yīng)用開(kāi)發(fā)中,經(jīng)常需要處理來(lái)自不同數(shù)據(jù)庫(kù)的數(shù)據(jù),為了滿(mǎn)足這一需求,我們可以通過(guò)配置多個(gè)數(shù)據(jù)源來(lái)實(shí)現(xiàn)對(duì)不同數(shù)據(jù)庫(kù)的訪問(wèn),下面我們來(lái)看看具體實(shí)現(xiàn)吧
    2025-01-01
  • Java實(shí)現(xiàn)LRU緩存算法的參考示例

    Java實(shí)現(xiàn)LRU緩存算法的參考示例

    這篇文章主要介紹了JAVA實(shí)現(xiàn)LRU緩存算法的參考示例,幫助大家根據(jù)需求實(shí)現(xiàn)算法,對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-05-05
  • 教你怎么用SpringBoot整合Swagger作為API

    教你怎么用SpringBoot整合Swagger作為API

    這篇文章主要介紹了教你怎么用SpringBoot整合Swagger作為API,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Vue實(shí)現(xiàn)驗(yàn)證碼登錄的超詳細(xì)步驟

    Vue實(shí)現(xiàn)驗(yàn)證碼登錄的超詳細(xì)步驟

    這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)驗(yàn)證碼登錄的超詳細(xì)步驟,我們?cè)谑褂胿ue進(jìn)行前端開(kāi)發(fā)時(shí)都需要登錄驗(yàn)證,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • java 讀取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解

    java 讀取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解

    這篇文章主要介紹了java 讀取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Java中的NoSuchMethodException異常原因以及解決方案詳解

    Java中的NoSuchMethodException異常原因以及解決方案詳解

    這篇文章主要介紹了Java中的NoSuchMethodException異常原因以及解決方案詳解,NoSuchMethodException是Java反射機(jī)制中的異常,在嘗試通過(guò)反射獲取方法時(shí),找不到指定的方法,通常發(fā)生在調(diào)用?Class?對(duì)象的方法時(shí),當(dāng)方法名或方法參數(shù)不匹配時(shí)拋出該異常,需要的朋友可以參考下
    2024-02-02

最新評(píng)論