[Spring]: Inversion of Control
spring
08/12/2019
Example wihout IoC
Define interface
JAVA
public interface Player{ public String getWorkout();}
Define class implements Player
JAVA
public class SoccerPlayer implements Player{ @Override public String getWorkout() { return "kick the ball 100 times"; }}
Class with main method
JAVA
public class Demo { public static void main(String[] args) { Player player = new SoccerPlayer(); System.out.println(player.getWorkout()); }}
- Above snippets are basic object-oriented programming without using Spring's inversion of control
- Spring allows the app to be more configurable
- For example, if I want to create another sub-class BasketballPlayer the code need to be hardcoded as:
JAVA
Player player = new BasketballPlayer();
- But with the use of Inversion of Control, we can read the implementation name from a config file without changing the sourcecode
Examples with IoC:
Link: xml configuration