Thursday, 11 May 2017

Spring: @Configuration : Removing xml based configuration

Till now, all my previous examples used xml based configuration. Spring provide @Configuration annotation, you can enable java based configuraiton.

How to use @Configuration annotation?
Define a class and annotate it with @Configuration annotation. Define all the beans inside this class using @Bean annotation.

For example,
<bean id="osho" class="com.sample.pojo.Author">
 <property name="firstName" value="" />
 <property name="lastName" value="Jain" />
 <property name="dateOfBirth" value="11 December 1931" />
 <property name="country" value="India" />
</bean>

Above bean can be defined like below.
        @Bean
 public Author getAuthor() {

  Author author = new Author();

  author.setFirstName("Chandra Mohan");
  author.setLastName("Jain");
  author.setDateOfBirth("11 December 1931");
  author.setCountry("India");

  return author;
 }

Find the following complete working example.


Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 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;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

ApplicationConfig.java
package com.sample.configurations;

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

import com.sample.pojo.Author;

@Configuration
public class ApplicationConfig {

 @Bean
 public Author getAuthor() {

  System.out.println("Author bean is called");

  Author author = new Author();

  author.setFirstName("Chandra Mohan");
  author.setLastName("Jain");
  author.setDateOfBirth("11 December 1931");
  author.setCountry("India");

  return author;
 }
}


HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sample.configurations.ApplicationConfig;
import com.sample.pojo.Author;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

  Author author1 = context.getBean(Author.class);
  Author author2 = context.getBean(Author.class);
  Author author3 = context.getBean(Author.class);

  System.out.println(author1);
  System.out.println(author2);
  System.out.println(author3);

  ((AnnotationConfigApplicationContext) context).close();
 }
}

Output
Author bean is called
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]



Previous                                                 Next                                                 Home

No comments:

Post a Comment