一、作用
Spring 4.2 利用@Order
控制配置类的加载顺序
二、代码
package com.example.demo.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/**
* @author 2019/6/26 20:45
*/
@Configuration
@Order(2)
public class Demo1Controller {
@Bean
public void demo1Service(){
System.out.println("demo1config 加载了");
}
}
package com.example.demo.controller;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/**
* @author 2019/6/26 20:46
*/
@Configuration
@Order(1)
public class Demo2Controller {
@Bean
public void demo2Service(){
System.out.println("demo2config 加载了");
}
}
package com.example.demo.controller;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author 2019/6/26 20:53
*/
public class Client {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.example.demo.controller");
}
}
三、结果
demo2config 加载了
21:06:04.208 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'demo1Service'
demo1config 加载了
Disconnected from the target VM, address: '127.0.0.1:62082', transport: 'socket'