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
package ComponentAnnotation;
public class College {
public void test()
{
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
package ComponentAnnotation;
public class College {
public void test()
{
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
package ComponentAnnotation;
public class College {
public void test()
{
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
package BeanAnnotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CollegeConfig {
@Bean
public College collegeBean() {
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
package ComponentAnnotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args)
{
ApplicationContext context
= new AnnotationConfigApplicationContext(
CollegeConfig. class );
College college
= context.getBean( "collegeBean" , College. class );
college.test();
}
}
|
Output:
Test College Method
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 May, 2022
Like Article
Save Article