0% found this document useful (0 votes)
3 views

Inversion_of_Control_with_Example

Inversion of Control (IoC) is a design principle where the control of object creation and dependency management is handled by a container or framework, such as Spring. The document illustrates the difference between manual object creation and IoC using a Car and Engine example, highlighting how Spring manages dependencies through configuration in an XML file. The provided code demonstrates how to set up and use the IoC container to create and manage the lifecycle of application objects.

Uploaded by

Aditya Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Inversion_of_Control_with_Example

Inversion of Control (IoC) is a design principle where the control of object creation and dependency management is handled by a container or framework, such as Spring. The document illustrates the difference between manual object creation and IoC using a Car and Engine example, highlighting how Spring manages dependencies through configuration in an XML file. The provided code demonstrates how to set up and use the IoC container to create and manage the lifecycle of application objects.

Uploaded by

Aditya Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Inversion of Control (IoC) in Spring

Definition:

Inversion of Control is a principle in which the control of object creation and dependency

management is transferred from the program to a container or framework. In Spring, the IoC

container manages the lifecycle and configuration of application objects.

Example:

Without IoC (Manual Object Creation)

------------------------------------

public class Car {

private Engine engine;

public Car() {

this.engine = new Engine(); // tight coupling

public void drive() {

engine.start();

System.out.println("Car is driving");

With IoC (Spring Framework)

----------------------------
Engine.java

-----------

public class Engine {

public void start() {

System.out.println("Engine started");

Car.java

--------

public class Car {

private Engine engine;

// Constructor Injection

public Car(Engine engine) {

this.engine = engine;

public void drive() {

engine.start();

System.out.println("Car is driving");

beans.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">

<bean id="engine" class="Engine" />

<bean id="car" class="Car">

<constructor-arg ref="engine"/>

</bean>

</beans>

Main.java

---------

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Car car = (Car) context.getBean("car");

car.drive();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy