In this post under Spring Core, I will explain with example how to import multiple “@Configuration” classes into one master “@Configuration” class.
In my previous posts, I have created “@Configuration” annotated classes and defined one or two beans in them for my example.
Whereas in production code, there will be many bean definitions more than 100.
We can define all the bean definitions in one “@Configuration” annotated class but it is better to divide them into multiple “@Configuration” annotated classes.
For example we can move all security related bean definitions to “SecurityConfiguration” class which is annotated with “@Configuration” annotation.
Then we can have another class named “LoggingConfiguration” annotated with “@Configuration” annotation containing logging related bean definitions.
We then combine both the configuration class into one master configuration class using “@import” annotation.
Let see an example.
For our example lets say we have to two beans. Below is the class structure of both the beans
Bean1 and Bean2
package package3;
public class Bean1 {
@Override
public String toString() {
return "Hello its Bean1";
}
}
package package3;
public class Bean2 {
@Override
public String toString() {
return "Hello its Bean2";
}
}
Now we create two “@Configuration” annotated classes as shown below
SecurityConfiguration and LoggingConfiguration
package package3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SecurityConfiguration {
@Bean
public Bean1 bean1() {
return new Bean1();
}
}
package package3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoggingConfiguration {
@Bean
public Bean2 bean2() {
return new Bean2();
}
}
Now we will combine both “@Configuration” classes into a master configuration class as shown below
Master Configuration
1 package package3;
2
3 import org.springframework.context.annotation.Configuration;
4 import org.springframework.context.annotation.Import;
5
6 @Configuration
7 @Import({SecurityConfiguration.class, LoggingConfiguration.class})
8 public class BeanConfigurations {
9
10 }
In the above code, we have defined a master configuration class “BeanConfigurations” and annotated it with “@Configuration” annotation and “@import” annotation. Refer to line 6 and 7.
Marking the class “@Configuration” is required to inform Spring that it is a configuration class.
“@Import” annotation imports the bean definitions defined in the listed configuration class to the master configuration.
Now all the bean definition defined in “SecurityConfiguration” and “LoggingConfiguration” is imported in the master configuration.
Now we can create beans of “Bean1” and “Bean2” just using “BeanConfigurations” class as shown in the below main method
Main Class
1 package package3;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5
6 public class Example3 {
7 public static void main(String[] args) {
8 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfigurations.class);
9 Bean1 bean1 = applicationContext.getBean("bean1", Bean1.class);
10 System.out.println("Bean1: " + bean1);
11 Bean2 bean2 = applicationContext.getBean("bean2", Bean2.class);
12 System.out.println("Bean2: " + bean2);
13 }
14 }
In the above code we are creating the application context only by reading “BeanConfigurations” class (internally it reads both “SecurityConfiguration” and “LoggingConfiguration”).
We don’t need to specify the “SecurityConfiguration” and “LoggingConfiguration” classes explicitly.
In this way we can create separate configuration classes and then combine them in one master configuration.
Output
Bean1: Hello its Bean1
Bean2: Hello its Bean2