Spring Java Configuration Example

By | 2015-12-24T14:12:10+00:00 December 24th, 2015|Spring|1 Comment

Spring allows us to configure bean definitions in Java class. From Spring3 it supports Java configuration instead of xml based configuration. In this tutorial, we are going to implement the Spring Java Configuration.

Before going to implementation, lets see the differences between the spring xml configuration and spring Java configuration.

In Xml based configuration, we configure all beans in xml file with <bean> tag. Here is the typical xml configuration.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <context:component-scan base-package="com.onlinetutorialspoint.beans" />
    <context:annotation-config />
    <bean id="author" class="com.onlinetutorialspoint.beans.Author">
        <property name="authorName" value="Craig Walls" />
        <property name="authorAddress" value="Denton" />
    </bean>
    <bean id="book" class="com.onlinetutorialspoint.beans.Book">
        <property name="bookId" value="101" />
        <property name="bookName" value="Spring in Action" />
        <property name="author" ref="author" />
    </bean>
</beans>

On the above xml file, we configure Book and Author beans by using <bean> tag. And all the <bean> tags are configured under the <beans> tag.

Lets see, how spring Java configuration looks like for this beans.


@Configuration
public class BookConfiguration {
    Author author = null;
    Book book = null;

    @Bean
    public Author author() {
        author = new Author();
        author.setAuthorName("Craig Walls");
        author.setAuthorAddress("Denton");
        return author;
    }

    @Bean
    public Book book() {
        book = new Book();
        book.setBookId(101);
        book.setBookName("Spring in Action");
        book.setAuthor(this.author());
        return book;
    }

}

The above spring Java configuration file an exact equivalent configuration for xml file.  Here we can find some new spring annotations @Configuration and @Bean.

[box type=”info” align=”alignleft” class=”” width=”100%”]

@Configuration represents the <beans> tag in xml file.

@Bean annotation represents the <bean> tag.

[/box]

A complete example for Spring Java Configuration :

Here is the complete example for spring Java configuration:

Project Structure :

Spring Java Configuration

Required Dependencies :

pom.xml


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>    

Spring Beans :

Book.java


package com.onlinetutorialspoint.beans;

public class Book {
    private int bookId;
    private String bookName;
    private Author author;
    
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public Author getAuthor() {
        return author;
    }
    public void setAuthor(Author author) {
        this.author = author;
    }
    
}

Author.java


package com.onlinetutorialspoint.beans;

public class Author {
    private String authorName;
    private String authorAddress;

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public String getAuthorAddress() {
        return authorAddress;
    }

    public void setAuthorAddress(String authorAddress) {
        this.authorAddress = authorAddress;
    }

}

Spring Java Configuration File:

BookConfiguration.java


package com.onlinetutorialspoint.confg;

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

import com.onlinetutorialspoint.beans.Author;
import com.onlinetutorialspoint.beans.Book;

@Configuration
public class BookConfiguration {
    Author author = null;
    Book book = null;

    @Bean
    public Author author() {
        author = new Author();
        author.setAuthorName("Craig Walls");
        author.setAuthorAddress("Denton");
        return author;
    }

    @Bean
    public Book book() {
        book = new Book();
        book.setBookId(101);
        book.setBookName("Spring in Action");
        book.setAuthor(this.author());
        return book;
    }

}

Well ! complete. Its time to run the application.

Main.java


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

import com.onlinetutorialspoint.beans.Author;
import com.onlinetutorialspoint.beans.Book;
import com.onlinetutorialspoint.confg.BookConfiguration;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ctx = new AnnotationConfigApplicationContext(BookConfiguration.class);
        Book book = (Book) ctx.getBean(Book.class);
        Author author =  book.getAuthor();
        System.out.println("Book Name: "+book.getBookName());
        System.out.println("Author Name : "+author.getAuthorName());
        System.out.println("Author Address "+author.getAuthorAddress());
    }

}

Output:

[box type=”success” align=”alignleft” class=”” width=”100%”]

Book Name: Spring in Action
Author Name : Craig Walls
Author Address Denton

[/box]

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

One Comment

  1. raichand70@yahoo.ca'
    Raichand January 25, 2017 at 1:56 pm - Reply

    Hi,
    Please write a spring and JSF web application example in java configuration.

    Thanks

    Raichand

Leave A Comment