Registering multiple @Configuration annotated Classes

In the previous post under Spring Core, I explained with example the purpose of “@Configuration” annotation.

Just for recap, classes marked with “@Configuration” annotation are informing the Spring framework that it contains bean definitions.

Let’s call class marked with “@Configuration” as “Configuration Class”. It will help you in understanding this post.

Spring then uses “AnnotationConfigApplicationContext” class which read the configuration class and creates application context as shown below


    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example1.class);

In the above code, “Example1” is a class marked with “@Configuration” annotation. We are passing the Class object of “Example1” as an argument to “AnnotationConfigApplicationContext” constructor.

Now Spring doesn’t put any restriction on number of bean definitions a “Configuration Class” can have.

But for readability purpose it is better to split bean definitions in multiple “Configuration Classes” based on some categories. For example
1) SecurityConfiguration class that will have bean definitions that handle security of the application.
2) DatabaseConfiguration class that will have bean definitions that handle database access of the application.

etc

Now since we have multiple configuration class. How we should tell Spring to read multiple configuration class and create an application context.

For this purpose, Spring provides an overloaded version of “AnnotationConfigApplicationContext” that takes varargs as a parameter.

To this constructor we can pass multiple classes annotated with “@Configuration” annotation.

Below is the complete code for your reference. For our example we will define two beans.

Bean 1


package package2;

public class Bean1 {
    @Override
    public String toString() {
        return "Hello its Bean1";
    }
}

Bean 2


package package2;

public class Bean2 {
    @Override
    public String toString() {
        return "Hello its Bean2";
    }
}

Now we will define bean definitions for the above classes in two separate configuration classes as shown below

Configuration 1


package package2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Configuration1 {
    @Bean
    public Bean1 bean1() {
        return new Bean1();
    }
}

Configuration 2


package package2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Configuration2 {
    @Bean
    public Bean2 bean2() {
        return new Bean2();
    }
}

Next in the main class we will show how to tell Spring to read from multiple configuration classes

Main class


1  package package2;
2  
3  import org.springframework.context.ApplicationContext;
4  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5  
6  public class Example2 {
7      public static void main(String[] args) {
8          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration1.class, Configuration2.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 main class, at line 8, we are using overloaded version of “AnnotationConfigApplicationContext” constructor which takes varargs parameter and giving a list of previously defined configuration
classes Class object.

“AnnotationConfigApplicationContext” will read all the configuration classes passed as parameter and creates an application context.

In this way, we can register multiple configuration class with Spring.

Leave a Reply