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
- XML-based configuration.
- 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
@Componentduring 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”)).

