Skip to content
Related Articles

Related Articles

Spring @Configuration Annotation with Example

View Discussion
Improve Article
Save Article
  • Last Updated : 02 May, 2022

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework. So let’s understand @Configuration Annotation with an example project.

Implementation: Project

Suppose we have already a Java project and all the Spring JAR files are imported into that project. Now let’s create a simple class named College and inside the class, we have a simple method.

Prerequisite: Spring @ComponentScan Annotation with Example

A. File: College.java

Java




// Java Program to Illustrate College Class
 
package ComponentAnnotation;
 
// Class
public class College {
 
    // Method
    public void test()
    {
        // Print statement
        System.out.println("Test College Method");
    }
}

 
 

We can use @Component annotation for creating the bean for this class. So we can modify our College.java file something like this.

 

Java




// Java Program to Illustrate College Class
 
package ComponentAnnotation;
 
// Class
public class College {
 
    // Method
    public void test()
    {
        // Print statement
        System.out.println("Test College Method");
    }
}

 
 

But in this case, we have to write the following line inside the beans.xml file.

 

<context:component-scan base-package="ComponentAnnotation"/>

 

But we don’t want to use the complete beans.xml file in our project. So what we can do to replace the beans.xml file? In general, beans.xml is a configuration file. So what we can do is we can create a configuration class in Java and just make this class our Configuration class by just using the @Configuration Annotation. Yes, we can do that. So now let’s create another class named CollegeConfig.

 

B. File: CollegeConfig.java

 

Java




// Java Program to Illustrate College Class
 
package ComponentAnnotation;
 
// Class
public class College {
 
    // Method
    public void test()
    {
        // Print statement
        System.out.println("Test College Method");
    }
}

 
 

So now, we don’t want to use the @Component and @ComponentScan annotations to create the beans. Let’s discuss another way of doing the same task. So we are going to create the spring beans using the @Bean annotation. But how? Where to write these methods? As we have discussed at the beginning that “@Configuration annotation indicates that the class has @Bean definition methods”, so let’s explain this statement and create our beans inside the CollegeConfig.java file using the @Bean annotation. So we can write something like this inside our CollegeConfig.java file. Please refer to the comments for a better understanding. 

 

@Bean
// Here the method name is the
// bean id/bean name
public College collegeBean(){
    // Return the College object
    return new College();
}

 

Yes, that’s it. 

 

C. File: CollegeConfig.java

 

Java




// Java Program to Illustrate Configuration Class
 
package BeanAnnotation;
 
// Importing required classes
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class CollegeConfig {
 
      // Using Bean annotation to create
    // College class Bean
    @Bean
    // Here the method name is the
    // bean id/bean name
    public College collegeBean() {
       
        // Return the College object
        return new College();
    }
 
}

 
 

Similarly, if you have another class named Student and you want to create the bean for this Student class then you can create the bean using the @Bean annotation inside the configuration class just like that

 

@Bean
// Here the method name is the
// bean id/bean name
public Student studentBean(){
    // Return the Student object
    return new Student();
}

 

Now to check our application let’s create a main method inside our Main class. Below is the code for the Main.java file. Comments are added inside the code to understand the code in more detail.

 

D. File: Main.java

 

Java




// Java Program to Illustrate Application Class
 
package ComponentAnnotation;
 
// Importing required classes
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
// Application class
public class Main {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Use AnnotationConfigApplicationContext
        // instead of ClassPathXmlApplicationContext
        // because we are not using XML Configuration
        ApplicationContext context
            = new AnnotationConfigApplicationContext(
                CollegeConfig.class);
 
        // Getting the bean
        College college
            = context.getBean("collegeBean", College.class);
 
        // Invoking the method
        // inside main() method
        college.test();
    }
}

 
 

Output:

 

Test College Method

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!