基于SpringBoot开机启动与@Order注解

 更新时间:2021年09月15日 10:48:06   作者:总是幸福的老豌豆  
这篇文章主要介绍了SpringBoot开机启动与@Order注解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringBoot开机启动与@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标记

@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/

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java简单几步实现一个二叉搜索树

    Java简单几步实现一个二叉搜索树

    二叉树包含了根节点,孩子节点,叶节点,每一个二叉树只有一个根节点,每一个结点最多只有两个节点,左子树的键值小于根的键值,右子树的键值大于根的键值,下面这篇文章主要给大家介绍了关于如何在Java中实现二叉搜索树的相关资料,需要的朋友可以参考下
    2023-02-02
  • 关于@Value取值为NULL的解决方案

    关于@Value取值为NULL的解决方案

    这篇文章主要介绍了关于@Value取值为NULL的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Spring Security添加验证码的两种方式小结

    Spring Security添加验证码的两种方式小结

    使用spring security的时候,框架会帮我们做账户密码的验证,但是如我们需要添加一个验证码,就需要对配置文件进行修改,这篇文章主要给大家介绍了关于Spring Security添加验证码的两种方式,需要的朋友可以参考下
    2021-10-10
  • MyBatis传入多个参数时parameterType的写法

    MyBatis传入多个参数时parameterType的写法

    这篇文章主要介绍了MyBatis传入多个参数时parameterType的写法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 浅谈Java中随机数的几种实现方式

    浅谈Java中随机数的几种实现方式

    这篇文章主要介绍了Java中随机数的几种实现方式,从最简单的Math.random到多线程的并发实现都在本文所列之中,需要的朋友可以参考下
    2015-07-07
  • Java abstract class 与 interface对比

    Java abstract class 与 interface对比

    这篇文章主要介绍了 Java abstract class 与 interface对比的相关资料,需要的朋友可以参考下
    2016-12-12
  • SimpleDateFormat线程安全问题排查详解

    SimpleDateFormat线程安全问题排查详解

    这篇文章主要为大家介绍了SimpleDateFormat线程安全问题排查详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • java并发包JUC诞生及详细内容

    java并发包JUC诞生及详细内容

    这篇文章主要为大家介绍了java并发包JUC的诞生及JUC增加的内容详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • SpringBoot中使用拦截器的配置详解

    SpringBoot中使用拦截器的配置详解

    这篇文章主要介绍了SpringBoot中使用拦截器的配置详解,拦截器是 AOP 的一种实现,专门拦截对动态资源的后台请求,即拦截对控制层的请 求,使用场景比较多的是判断用户是否有权限请求后台,需要的朋友可以参考下
    2024-01-01
  • Java多线程实现聊天客户端和服务器

    Java多线程实现聊天客户端和服务器

    这篇文章主要为大家详细介绍了Java多线程聊天客户端和服务器实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10

最新评论