• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-09) 37次浏览 已收录 0个评论
文章目录[隐藏]
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.Compon<p>本文来源gao!daima.com搞$代!码#网#</p>ent;/** * @Classname BootCommandLineRunner * @Description TODO * @Date 2020/3/6 13:09 * @Created by zhaocunwei */@Order(1)@Slf4j@Componentpublic 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})@Documentedpublic @interface Order

这个标记包含一个value属性。属性接受整形值。如:1,2 等等。值越小拥有越高的优先级。

下面让我们来看一个

使用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@Componentpublic 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@Componentpublic class BootCommandLineRunner implements CommandLineRunner {    @Override    public void run(String... args) throws Exception {        log.info("This is BootCommandLineRunner ...");    }}

spring @Order标记

@Order标记定义了组件的加载顺序


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:基于SpringBoot开机启动与@Order注解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址