@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 –

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- bean definition -->
   <bean id = "mybeanexample1" class = "com.somepackage">
      
   </bean>

</bean>

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.

@Configuration
public class ApplicationConfig {
        @Bean
	public ExampleBean getExampleBean()  {
		
		return new ExampleBean();
	}
}

 

Another example where we are defining a bean for DataSource-

@Configuration
public class ApplicationConfig {
     @Bean
	public DataSource dataSource() {
		DataSource dataSource = null; 
		System.out.println("value of datasource"+dataSource);
		try {
	   
	    Context initialContex = new InitialContext();
	    System.out.println("value of datasource"+dataSource);
	    
	    dataSource = (DataSource)(initialContex.lookup("java:jboss/datasources/BootWarDeployment"));
	    
	    System.out.println("value of datasource"+dataSource);
	    
	    if(dataSource != null) {
	    	dataSource.getConnection();
		    
	    }
	    
		}catch(Exception e) {
			e.printStackTrace();
		}
	   
	    return 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.

 

package com.bootwar.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebAppAdapterConfiguration extends WebMvcConfigurerAdapter{
	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		
		configurer.defaultContentType(MediaType.APPLICATION_XML);
		// We can configure what kind of data we want to show
		//configurer.defaultContentType(MediaType.APPLICATION_JSON);
		//configurer.defaultContentType(MediaType.APPLICATION_FORM_URLENCODED);
	}

}

 

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


    @Override
	public void addFormatters(FormatterRegistry registry) {
        //Some more logic
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
         //Some more logic
	}

	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //Some more logic
	}

 

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.