Spring Bean Configuration with Annotation

Java Annotation:
   • It is a special label/marker added to Java classes
   • It provides meta-data about class
   • It is Processed at compile time or runtime for special processing

Why do we use  Spring Configuration with Annotation?
XML configuration can be verbose (Large projects can have lots of Spring Beans that can be configured in the XML file. It will take lots of work. So, configure your Spring Beans with Annotation). Annotation minimizes the XML configuration

In this project example, we will create a POJO class i.e., CricketCoach.java.In this POJO class, we will create a method that returns a message. We will create an XML configuration i.e., applicationContext.xml file. In this XML file, we will define a base package of the project. Spring Container scan the base package and its sub-packages to identify which  POJO class is a Spring Bean to configure and manage it. In this way, we will don’t each and every Spring Bean to configure in the XML configuration file. We will create the main class i.e., ClientApp.java which contains code for active Spring Container and load the applicationContex.xml file. Then we will get a Spring Bean class and access its method to print output on the console.      

Development Process:
1. Keep eclipse IDE ready
2. Project Structure
3. Enable component scanning in the Spring  XML config file
4. Add the jar file to the Build Path of  the Project
5. Create a POJO Class and Add the @Component Annotation
6. Create Main Class
7. Run the App

1. Keep eclipse IDE ready
2. Project Structure


3. Enable component scanning in the Spring XML config file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     <!-- enable component scanning -->
     <context:component-scan base-package="com.poc"/>

→ <context:component-scan base-package=”base package”/> from this tag Spring will scan this package and identify which class have @Component.Then that class will register on Container with bean id.

4. Add the jar file to the Build Path of the Project
Right Click on Project(Spring_Bean_Config_With_JavaAnnotation) ⇒ Build Path ⇒ Java Build Path ⇒ libraries ⇒ Add External jars


5. Create a POJO Class and Add the @Component Annotation
CricketCoach.java (Pojo Class):

package com.poc.bean;
import org.springframework.stereotype.Component;

public class CricketCoach {
	public String getDailyWorkout(){
		return "4 hours of Batting Practice";

→ @Component(“-”) will make a java class as Spring Bean with bean Id.
→ Spring Container will register this class automatically.
→ @Component is having multiple variations provided by Spring Framework with minor changes functionality specific like:
   • @Controller: This annotation is used when we are working Web Application or REST API based. This annotation makes a normal class the Controller of the application. This controller class responsibility is delegated from one layer to another.
   • @Service: This annotation is used when the class is making a service class. In this, we can write code with the business logic of the application.
   • @Respository: This annotation is used when we are working with persistence logic related to Databases like CRUD(Create Read Update Deleted) functionality specific.
→ These are three variations of the @Component annotation. These are all functionality-specific annotations only where needed we can use these annotations according to our requirements in the real-time application.
→ These four annotations(@Component,@Service,@Repository and @Controller) are available in the org.springframework.stereotype package.   
→ These are regularly when we are working on MVC(Model-View-Controller) pattern applications.
→ When we are using any one of these annotations Spring container will be treated as Spring Bean.

6. Create Main Class

package com.poc.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.poc.bean.Cricket Coach;

public class ClientApp {
public static void main(String[] args) {
//load the spring Configuration file
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//retrieve bean from spring container
CricketCoach theCoach=context.getBean("coach",CricketCoach.class);
// call the method on bean

7. Run the App

4 hours of Batting Practice

This topic is explained What is Java Annotation? How to configure Spring Bean with Annotation? How to enable component scanning in the config file? How to retrieve a bean from a Spring container?

Leave a Comment