SpringBoot开机启动与@Order注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 ..." );
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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 标记定义如下:
1
2
3
4
|
@Retention (value=RUNTIME)
@Target (value={TYPE,METHOD,FIELD})
@Documented
public @interface Order
|
这个标记包含一个value属性。属性接受整形值。如:1,2 等等。值越小拥有越高的优先级。
下面让我们来看一个
使用spring 3.x 和spring 4.x 的例子
Ranks.java
1
2
3
|
package com.javapapers.spring3.autowire.collection;
public interface Ranks {
}
|
RankOne.java
1
2
3
4
5
6
7
8
9
10
11
|
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
1
2
3
4
5
6
7
8
9
10
11
|
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
1
2
3
4
5
6
7
8
9
10
11
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
4
5
6
7
8
9
10
11
|
<? 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
1
2
3
4
5
6
7
8
9
10
|
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标记,值做相应的调整。
1
2
3
4
5
6
7
8
|
@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/
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://zhaocunwei.blog.csdn.net/article/details/104694000