You are on page 1of 1

Spring Inversion of Control

In spring, we’ll often hear about IoC (Inversion of Control). So, what actually the IoC means ?

I found out a definition for it as “The approach of outsorcing the creation and management of the
objects”

The key points : App should be configurable, to switching context without changing the actual
code

Step :

1. Setup the beans :


- Create an xml file (one way to do it)
Create a <bean> key with id and class attributte (e.g : <bean id=”myCoach”
class=”com.reynaldi.springdemo.BaseballCoach”></bean>) , where class value
need to be a fully-qualified name for a Class

2. Retrieve value from the class :


- Create ClasshPathXmlApplicationContext object, will require you to pass the xml
path as a constructor parameter
- Create an object of the interface that get value from get.Bean() method from
ClassPathXmlApplicationContext object. The get.Bean() method will need two
value as arguments, that is the bean id and the interface name (e.g
context.getBean(“myCoach”, Coach.class) where Coach is an interface name

See that you don’t have to change anything inside your code, just need to change the value in
your application context xml file

You might also like