Saturday, 14 July 2018

Spring boot: Configuration annotation example

In this post, you are going to learn
a.   What is Configuration annotation
b.   How to use configuration annotation
c.   Example of Configuration annotation

What is Configuration annotation
@Configuration annotation is used on a class to define beans. Spring scans all the classes that are annotated with @Configuration annotation and initialize the beans.


How to use Configuration annotation?
Spring recommends, “the class that defines the main method is a good candidate as the primary @Configuration”.

In this tutorial, I am defining new class (other than main class) EmployeeConfiguration, that define all the beans specific to my application. Spring scans this class and take care of beans creation.

Example of Configuration annotation

Employee.java
package com.sample.myApp.model;

import org.springframework.stereotype.Component;

@Component
public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}

EmployeeConfiguration.java
package com.sample.myApp.Configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sample.myApp.model.Employee;

@Configuration
public class EmployeeConfiguration {

 @Bean
 public Employee getEmployee() {
  Employee emp = new Employee();
  emp.setFirstName("Siva Krishna");
  emp.setId(1);
  emp.setLastName("Ponnam");
  return emp;
 }
 
 @Bean
 public Employee getDefaultEmployee() {
  Employee emp = new Employee();
  emp.setFirstName("NO_NAME");
  emp.setId(-1);
  emp.setLastName("NO_NAME");
  return emp;
 }
}

Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ApplicationContext;

import com.sample.myApp.model.Employee;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {
  ApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  Employee employee = (Employee) applicationContext.getBean("getEmployee");
  Employee defaultEmployee = (Employee) applicationContext.getBean("getDefaultEmployee");
  
  System.out.println(employee);
  System.out.println(defaultEmployee);
 }
}


When you ran Application.java, you can able to see below output in the console.

Employee [id=1, firstName=Siva Krishna, lastName=Ponnam]
Employee [id=-1, firstName=NO_NAME, lastName=NO_NAME]

Note
You can add @Configuration annotation on a Class, interface (including annotation type), or enum declaration.



Previous                                                 Next                                                 Home

No comments:

Post a Comment