@Configuration Class in Spring

@Configuration annotation is used with a class to indicate that it has methods with @Bean annotation inside it.

Methods with @Bean annotations signify that they are bean definition methods and Spring IOC container would process those methods and generate spring beans.

Let us look at an example below.

@Configuration
Class Employee
{

  @Bean
  public Manager getMgr(){
     return new Manager();
  }

  @Bean
  public Clerk getClrk(){
     return new Clerk();
  } 

}

As per the above example, two beans would be created with Manager and Clerk objects by Spring.

Now consider a situation. What if the @Configuration annotation is removed? Would Spring recognize the bean definition methods?.

The answer is Yes. Beans would be created by the Spring IOC Container but if you call the bean definitions method for example getMgr(), then it will give you an additional instance of the Manager Class and the class would no longer be singleton. The method would not return the existing instance of the bean which spring created.

Hence the @Configuration annotation has a strong purpose.