Spring Bean Configuration with Java Code (No Xml)

We can configuration in three different ways are:
•Spring Bean configuration with XML(applicationContext.xml)
•Spring Bean configuration Java Annotation(@Component) and XML (applicationContext.xml)
•Spring Bean with Java code (@Configuration and @Bean)

If we are configuring Spring bean with XML. In this way, we will add an entry for every POJO class in the XML configuration file. So, the Spring container configures, manages, and instantiates that class as Spring Bean. But in this, we will do a lot of work and time to configure the class in the XML config file. If we are configuring Spring bean through Java annotation. In this configuration based, we can annotate the classes with the “@Component” annotation to make a Spring bean class. In an XML configuration file, we will configure the base package of the application only. So, the Spring container scans the package configure Spring Bean class which is annotated with the “@Component” annotation of the application. This is better than the Spring Bean with XML configuration file. Because there is no need for every POJO class to add an entry in the XML configuration file. If we are configuring Spring Bean through the Java code. In this way, there is no need for XML configuration for Spring Bean. We can configure all Spring Bean classes through annotation. This way is better than Spring Bean with XML configuration, Spring bean with Java annotation, and XML configuration file.

In this example we can configure Spring Bean with Java Source without XML configuration for that we can use @Configure, @Bean, and AnnotationConfigApplicationContext class.
@Configuration: This will make the class the configuration class of the app (replacement of applicationContext.xml).
@Bean: This will make the Bean class register with bean id on Configuration class(replacement of <bean>—</bean>).
AnnotationConfigApplicationContext: Enable the IOC container and the Configuration class.

In this example of the project, we will create a POJO class i.e., CricketCoach.java which contains a method that returns a message. We will create a Java class i.e., AppConfig.java that annotates @Configuration annotation to treat it as a configuration class. In this, we will create a method i.e., “teamCoach()” which returns a CricketCoach class object which is annotated with @Bean annotated. So, the Spring Container is configuring the CricketCoach.java class as Spring Bean. The main class i.e., ClientApp.java activates Spring Container and we will get Spring Bean. Then we will access a method of the Spring Bean and print output on the console.

Development Process:
    1. Keep eclipse IDE ready
    2. Project Structure
    3. Create a Java class
  4. Add the jar file to the Build Path of the Project for the example of the Spring Bean configuration with Java Code
    5. Create a  Java class and annotated it as @Configuration
    6. Create Main Class         
    7. Run the App

1. Keep eclipse IDE ready
2. Project Structure  

3. Create a Java class
CricketCoach.java(Pojo class):

package com.poc.bean;

public class CricketCoach {

  public String getDailyWorkout() {

    return "8 hours of Practice";
  }
}

4. Add the jar file to the Build Path of the Project for the example of the Spring Bean configuration with Java Code
Right Click on Project(Spring_Bean_Config_JavaCode(No Xml)) ⇒ Build Path  ⇒ Java Build Path ⇒ libraries ⇒ Add External jars

Commons-logging-<version>.jar
spring-beans-<version>.jar
spring-context-<version>.jar
spring-core-<version>.jar
spring-expression-<version>.jar
spring-aop-<version>.jar

5. Create a Java class and annotated it as @Configuration
AppCofig.java(Configuration class):

package com.poc.cfg;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.poc.bean.CricketCoach;

@Configuration

public class AppConfig {

  static {

    System.out.println("Static Block of Configuration class");

  }

  public AppConfig() {

    System.out.println("0-Param Constructor of Configuration class");

  }

  @Bean

  public CricketCoach teamCoach() {

    return new CricketCoach();

  }

}

→ @Bean IOC Container register as Spring Bean class default id is the method name e.g., “teamCoach()”.

6. Create Main Class
ClientApp.java:

package com.poc.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.poc.bean.CricketCoach;

import com.poc.cfg.AppConfig;

public class ClientApp {

  public static void main(String[] args) {

    // activate IOC container load and the spring Configuration file

    AnnotationConfigApplicationContext context

      = new AnnotationConfigApplicationContext(AppConfig.class);

    //retrieve bean from spring container

    CricketCoach teamCoach = context.getBean("theCoach", CricketCoach.class);

    // call the method on bean

    System.out.println(teamCoach.getDailyWorkout());

    context.close();

  }

}

7. Run the App
Output:

Static Block of Configuration class
0-Param Constructor of Configuration class
8 hours of Practice

Conclusion:
This example is explained How to create a Spring Bean Configuration in the java class? What is the use of @Configuration? What is the use of @Bean?

Leave a Comment