Skip to content

Commit 92522aa

Browse files
BAEL-9453 by sgrverma23: changes of ways to configure the Order of Configuration in Spring Boot (#18860)
* BAEL-9453 by sgrverma23: changes of ways to configure the Order of Configuration in Spring Boot * minor refactoring --------- Co-authored-by: sverma1-godaddy <[email protected]>
1 parent cfd57df commit 92522aa

File tree

14 files changed

+270
-1
lines changed

14 files changed

+270
-1
lines changed

spring-boot-modules/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>flyway-multidb-springboot</module>
2121
<module>spring-boot-admin</module>
2222
<module>spring-boot-angular</module>
23+
<module>spring-boot-config-order</module>
2324
<module>spring-boot-annotations</module>
2425
<module>spring-boot-annotations-2</module>
2526
<module>spring-boot-artifacts</module>
@@ -167,4 +168,4 @@
167168
</dependencies>
168169
</dependencyManagement>
169170

170-
</project>
171+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung.springbootconfigorder</groupId>
7+
<artifactId>spring-boot-config-order</artifactId>
8+
<version>1.0</version>
9+
<name>spring-boot-config-order</name>
10+
<packaging>jar</packaging>
11+
12+
<parent>
13+
<groupId>com.baeldung.spring-boot-modules</groupId>
14+
<artifactId>spring-boot-modules</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-maven-plugin</artifactId>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
39+
<properties>
40+
<start-class>com.baeldung.Application</start-class>
41+
</properties>
42+
43+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.application;
2+
3+
import com.baeldung.application.defaultconfig.ConfigA;
4+
import com.baeldung.application.defaultconfig.ConfigB;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
public class BeanOrderApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(new Class[]{BeanOrderApplication.class, ConfigA.class, ConfigB.class},
13+
args);
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.application.autoconfig;
2+
3+
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@AutoConfigureOrder(1)
9+
public class FirstAutoConfig {
10+
11+
@Bean
12+
public String autoBeanOne() {
13+
return "AutoBeanOne";
14+
}
15+
}
16+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.application.autoconfig;
2+
3+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@AutoConfigureAfter(FirstAutoConfig.class)
9+
public class SecondAutoConfig {
10+
11+
@Bean
12+
public String autoBeanTwo() {
13+
return "AutoBeanTwoAfterOne";
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.application.defaultconfig;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class ConfigA {
8+
9+
@Bean
10+
public String beanA() {
11+
return "Bean A";
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.application.defaultconfig;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class ConfigB {
8+
9+
@Bean
10+
public String beanB() {
11+
return "Bean B";
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.application.dependsonconfig;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.DependsOn;
6+
7+
@Configuration
8+
public class DependsConfig {
9+
10+
@Bean
11+
public String firstBean() {
12+
return "FirstBean";
13+
}
14+
15+
@Bean
16+
@DependsOn("firstBean")
17+
public String secondBean() {
18+
return "SecondBeanAfterFirst";
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.application.orderbased;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.annotation.Order;
6+
7+
@Configuration
8+
@Order(1)
9+
public class ConfigOne {
10+
11+
@Bean
12+
public String configOneBean() {
13+
return "ConfigOneBean";
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.application.orderbased;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.annotation.Order;
6+
7+
@Configuration
8+
@Order(2)
9+
public class ConfigTwo {
10+
11+
@Bean
12+
public String configTwoBean() {
13+
return "ConfigTwoBean";
14+
}
15+
}

0 commit comments

Comments
 (0)