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

Before going to implementation, let’s see the differences between the spring XML configuration and spring Java configuration.

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

spring.xml
<?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 the <bean> tag. And all the <bean> tags are configured under the <beans> tag.

Let’s see, how the spring Java configuration looks like for these beans.

BookConfiguration.java
@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 is an exact equivalent configuration for the XML file.
The @Configurationannotation represents the <beans> tag and the @Bean annotation represents the <bean> tag.

Spring @Configuration:

Here is the complete example for spring Java configuration:

Project Structure :

Spring Java Configuration

Required Dependencies :

pom.xml

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

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

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

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

}

Done!

Run the application.

Main.java

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:

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

References:

Happy Learning 🙂

Download Example