@Configuration annotation example using spring boot

In this post we will see @Configuration annotation example using spring boot.

  • @Configuration annotation introduced in Spring 3.0, available in org.springframework.context.annotation package.
  • We can have multiple configuration classes in our application as we can have multiple XML configuration files.
  • As the name suggests, we use this annotation for configuration purpose. We can have XML based or annotation based configuration in our application. Let’s see one possible scenario how to use @Configuration annotations. Suppose we want to define a bean and want to use later.

XML based approach –

Using @Configuration annotation –

Suppose we have a class ApplicationConfig.java where we have configuration related stuff. For example, creating datasource or we want to define different beans.


Another example where we are defining a bean for DataSource-


Let’s see some real time scenario where you can use @Configure annotation.

Suppose we want to configure WebMvcConfiureAdapter. This is a predefined class(available in org.springframework.web.servlet.config.annotation package) can be used for content negotiation. Here content negotiation means what type of data we want to produce as the response. We can configure using @Configuration annotation and then we need to override some method. Let’s see an example.



Similarly, we can configure formatters and resourcehandlers and other stuff.


Note – WebMvcConfiureAdapter class has been deprecated now.


Summary – @Configuration annotation is used to configure the different resources(Earlier we suppose to do the same task using XML ). Some common scenario where we used this annotation, use this annotation with class and create bean using @Bean annotation(for example – creating datasource bean, entityManagerBean, transactionManger and many more). Also, we can configure ContentNegotiation, Formatters, interceptors etc. While using @Configuration annotation it is good practice to define a separate class for different configuration purpose.

An example which shows how to use @Configuration annotation and create bean using @Bean annotation.

That’s all about @Configuration annotation example using spring boot. You may like.

Check @Configuration annotation docs here.