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

目录
  • SpringBoot开机启动与@Order注解
  • spring @Order标记
    • @Order标记定义了组件的加载顺序
    • 使用spring 3.x 和spring 4.x 的例子

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/

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

(0)

相关推荐

  • Spring Boot 项目启动自动执行方法的两种实现方式

    目录 实际应用场景: 第一种实现ApplicationRunner接口 第二种实现CommandLineRunner接口 对比: 注意: 实际应用场景: springboot项目启动成功后执行一段代码,如系统常量,配置.代码集等等初始化操作:执行多个方法时,执行顺序使用Order注解或Order接口来控制. Springboot给我们提供了两种方式 第一种实现ApplicationRunner接口 package org.mundo.demo.core; import org.springfra

  • 解决spring boot启动扫描不到自定义注解的问题

    对于自定义注解这里就不唠叨了,百度一大堆,这里有我一个自定义注解 @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface MsgEvent { RetailOrderEvent msgEvent(); } 注解实现类 @Component public class MsgEventProcessor implements BeanPostProcessor { /** * 事件消息

  • Spring Boot 2 实战:自定义启动运行逻辑实例详解

    本文实例讲述了Spring Boot 2 实战:自定义启动运行逻辑.分享给大家供大家参考,具体如下: 1. 前言 不知道你有没有接到这种需求,项目启动后立马执行一些逻辑.比如缓存预热,或者上线后的广播之类等等.可能现在没有但是将来会有的.想想你可能的操作, 写个接口上线我调一次行吗?NO!NO!NO!这种初级菜鸟才干的事.今天告诉你个骚操作使得你的代码更加优雅,逼格更高. 2. CommandLineRunner 接口 package org.springframework.boot; impo

  • 详解Spring Boot 项目启动时执行特定方法

    Springboot给我们提供了两种"开机启动"某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法.我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的. CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用App

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

    目录 SpringBoot开机启动与@Order注解 spring @Order标记 @Order标记定义了组件的加载顺序 使用spring 3.x 和spring 4.x 的例子 SpringBoot开机启动与@Order注解 package com.example.zcw.runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.spri

  • SpringBoot之Order注解启动顺序说明

    目录 Order注解启动顺序 order的规则 见下 它们的启动日志 @Order注解提供消费顺序 @org.springframework.core.annotation.Order Order注解启动顺序 order的规则 order的值越小,优先级越高 order如果不标注数字,默认最低优先级,因为其默认值是int最大值 该注解等同于实现Ordered接口getOrder方法,并返回数字. @Retention(RetentionPolicy.RUNTIME) @Target({Eleme

  • SpringBoot启动类@SpringBootApplication注解背后的秘密

    在用SpringBoot的项目的时候,会发现不管干什么都离不开启动类,他是程序唯一的入口,那么他究竟为我们做了什么?本篇文章主要解析@SpringBootApplication. 一.启动类 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } 二.@SpringBoo

  • 浅谈基于SpringBoot实现一个简单的权限控制注解

    注解是 JDK 5.0 引入的一种注释机制.注解可以作用在类型(类.接口.枚举等).属性.方法.参数等不同位置,具体的 JDK 版本所支持的注解位置可参考 java.lang.annotation.ElementType .此外还有注解的策略,也就是 RetentionPolicy ,这里不加赘述. 注解可以实现很多功能,其中最主要的就是进行代码标注,所以有时候注解也叫做标注.使用起来也基本顾名思义,就是对代码进行标注,简化部分代码的逻辑. 下面我们就着手实现一个简单的权限控制注解,来对注解有一

  • 解决springboot+activemq启动报注解错误的问题

    springboot+activemq启动报注解错误 Description: Field jmsMessagingTemplate in com.haozz.demo.mq.PromoteActProducer required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found. The injection point has the following anno

  • 基于Springboot一个注解搞定数据字典的实践方案

    目录 问题引出: 要求: 方案 实现 问题引出: 最近开了新项目,项目中用到了数据字典,列表查询数据返回的时候需要手动将code转换为name,到前台展示.项目经理表示可以封装一个统一的功能,避免程序员各自写各自的,代码混乱,风格不统一. 要求: 基于微服务架构,数据字典通过服务获取: 简化代码,使用简单: 使用Redis: 方案 大致的方向是自定义注解,在序列化的时候进行数据处理: 考虑到微服务,需要将主要逻辑放到common中,然后对外提供接口,各业务服务实现接口以获取字典数据: 考虑Red

  • 基于SpringBoot核心原理(自动配置、事件驱动、Condition)

    前言 SpringBoot是Spring的包装,通过自动配置使得SpringBoot可以做到开箱即用,上手成本非常低,但是学习其实现原理的成本大大增加,需要先了解熟悉Spring原理.如果还不清楚Spring原理的,可以先查看博主之前的文章,本篇主要分析SpringBoot的启动.自动配置.Condition.事件驱动原理. 正文 启动原理 SpringBoot启动非常简单,因其内置了Tomcat,所以只需要通过下面几种方式启动即可: @SpringBootApplication(scanBas

  • 基于SpringBoot与Mybatis实现SpringMVC Web项目

    一.热身 一个现实的场景是:当我们开发一个Web工程时,架构师和开发工程师可能更关心项目技术结构上的设计.而几乎所有结构良好的软件(项目)都使用了分层设计.分层设计是将项目按技术职能分为几个内聚的部分,从而将技术或接口的实现细节隐藏起来. 从另一个角度上来看,结构上的分层往往也能促进了技术人员的分工,可以使开发人员更专注于某一层业务与功能的实现,比如前端工程师只关心页面的展示与交互效果(例如专注于HTML,JS等),而后端工程师只关心数据和业务逻辑的处理(专注于Java,Mysql等).两者之间

  • 详解SpringBoot应用服务启动与安全终止

    SpringBoot应用服务启动 参照官方示例工程可以快速搭建简单SpringBoot应用,官方连接如下:http://projects.spring.io/spring-boot/#quick-start 闲话少叙,上代码: package hello; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype

  • SpringBoot整个启动过程的分析

    前言 前一篇分析了SpringBoot如何启动以及内置web容器,这篇我们一起看一下SpringBoot的整个启动过程,废话不多说,正文开始. 正文 一.SpringBoot的启动类是**application,以注解@SpringBootApplication注明. @SpringBootApplication public class CmsApplication { public static void main(String[] args) { SpringApplication.run

随机推荐