Close Menu
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Facebook X (Twitter) Instagram
Developers GroundDevelopers Ground
Facebook X (Twitter) Instagram
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Developers GroundDevelopers Ground
Home Spring IoC
Spring

Spring IoC

Akshay KulkarniBy Akshay KulkarniSeptember 10, 2023Updated:December 31, 2023No Comments3 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Reddit WhatsApp Email
Share
Facebook Twitter LinkedIn Pinterest WhatsApp Email

What is Spring IoC?

  • IoC stands for Inversion of Control.
  • It is the approach of outsourcing the construction and management of objects.
  • In software engineering, Spring IoC is the principle by which control of a program’s objects is transferred to the framework or container.
  • Developers commonly use it in object-oriented programming.

Explain the advantages of Spring IoC.

  • We can easily achieve decoupling between objects.
  • Makes it easier to switch between different implementations.
  • It provides greater modularity of a program.
  • We can easily test applications by isolating different components.

What is a Spring Bean?

  • A spring bean is a simple Java object.
  • Spring container creates Java objects and will be named Spring beans.
  • The Spring container creates Spring beans from normal Java classes.
  • The objects that are managed by the spring IoC container are known as spring beans.

Spring IoC Configuration

There are two ways to do IoC configuration

  1. XML-based configuration.
  2. Annotation-based configuration.

XML-based Configuration

Assuming you have a simple Java class

// MyDemo.java
public class MyDemo {
    public void displayMessage() {
        System.out.println("Message from MyDemo");
    }
}

Now, Let’s create an XML configuration file to define and configure Bean.

<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Define the MyDemoe bean -->
    <bean id="myDemo" class="com.example.MyDemo">
    </bean>

</beans>

Now, Let’s create a Java class with the main method to load the Spring Context and retrieve the spring bean.

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        // Load the Spring context from the XML configuration file
        ClassPathApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // Retrieve the MyService bean from the context
        MyDemo myDemo = context.getBean("myDemo", MyDemo.class);

        // Use the MyDemo bean
        myDemo.displayMessage();
    }
}

Annotation Based Configuration

Spring provides one more option to configure the IoC container which is nothing but an annotation-based configuration.

This is a preferred approach for configuration over XML-based configuration because of its conciseness and readability.

// MyService.java
@Component
public class MyService {
    public void displayMessage() {
        System.out.println("Message from MyService" );
    }
}

The Java class is annotating with the @Component annotation.

Working of @Component Annotation

  • It is a fundamental annotation in the Spring framework used to declare a Spring bean.
  • When a class is declared with @Component , It indicates to the Spring IoC container that it should manage instances of this class as Spring beans.
  • Spring automatically scans the classpath for classes annotated  @Component during the component scanning process.
  • By default, the bean name derives from the class name with the initial letter in lowercase (e.g., if the class name is MyDemo, then the default bean name is myDemo).
  • It is possible to provide a custom name for @Componentannotation (e.g. @Component(“service”)).

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
Akshay Kulkarni

Related Posts

Spring Boot Introduction

April 11, 2025

Introduction to Spring Framework

January 2, 2024

Spring Dependency Injection

December 31, 2023
Leave A Reply Cancel Reply

Featured Posts
  • 1
    Understanding the Java String Class
    • April 10, 2025
  • 2
    Array in Java
    • February 19, 2024
  • 3
    Control Statements
    • January 16, 2024
  • 4
    Java Objects
    • January 11, 2024
  • 5
    Java Constructors
    • January 2, 2024
Facebook X (Twitter) Instagram Pinterest
© 2025 Developers Ground, All rights reserved.

Type above and press Enter to search. Press Esc to cancel.