0% found this document useful (0 votes)
63 views11 pages

12 Spring Ioc Basics Overview

Spring Inversion of Control (IoC) allows defining objects (beans) in configuration rather than coding, and handles injecting dependencies. The Spring container manages object lifecycles using the configuration. Developers retrieve fully initialized objects from the container. Configuration can be via XML, annotations, or Java code. The process involves defining beans, creating a Spring container, and retrieving beans from the container.

Uploaded by

Rohit Solanki
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)
63 views11 pages

12 Spring Ioc Basics Overview

Spring Inversion of Control (IoC) allows defining objects (beans) in configuration rather than coding, and handles injecting dependencies. The Spring container manages object lifecycles using the configuration. Developers retrieve fully initialized objects from the container. Configuration can be via XML, annotations, or Java code. The process involves defining beans, creating a Spring container, and retrieving beans from the container.

Uploaded by

Rohit Solanki
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/ 11

Spring Inversion of Control

Ideal Solution
Object
give me a “Coach” object
Factory
My App

configuration

BaseballCoach CricketCoach
HockeyCoach

www.luv2code.com
Spring Container Spring

Object
give me a “Coach” object
Factory
My App

configuration

BaseballCoach CricketCoach
HockeyCoach

www.luv2code.com
Spring Container
Spring
• Primary functions Object
Factory
• Create and manage objects (Inversion of Control)

• Inject object’s dependencies (Dependency Injection)

www.luv2code.com
Configuring Spring Container

• XML configuration file (legacy, but most legacy apps still use this)


• Java Annotations (modern)


• Java Source Code (modern)

www.luv2code.com
Spring Development Process
Step-
B y -S
tep

1. Configure your Spring Beans


2. Create a Spring Container


3. Retrieve Beans from Spring Container

www.luv2code.com
Step 1: Configure your Spring Beans

File: applicationContext.xml

<beans … >

<bean id="myCoach"
class="com.luv2code.springdemo.BaseballCoach">
</bean>

</beans>

www.luv2code.com
Step 2: Create a Spring Container

• Spring container is generically known as ApplicationContext

• Specialized implementations Spring

• ClassPathXmlApplicationContext Object
Factory
• AnnotationConfigApplicationContext
• GenericWebApplicationContext
• others …

www.luv2code.com
Step 2: Create a Spring Container

ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");

www.luv2code.com
Step 3: Retrieve Beans from Container
Spring
Object
give me a “Coach” object
Factory
My App

configuration

BaseballCoach CricketCoach
HockeyCoach
www.luv2code.com
Step 3: Retrieve Beans from Container
// create a spring container
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");

// retrieve bean from spring container


Coach theCoach = context.getBean("myCoach", Coach.class);

File: applicationContext.xml
<bean id="myCoach"
class="com.luv2code.springdemo.BaseballCoach">
</bean>

www.luv2code.com

You might also like