Spring , one of the most popular DI (Dependency Injection) framework used in Enterprise now. There are so many feature of spring because of which it is using so much in market right now but the most important and the core feature is IOC i.e Inversion of Control.
So What is Inversion Of Control (important Point for Interview as well ) - So basically IOC is as words suggest the responsibility of Creating and Maitaining the lifecycle of Object is of Spring Container only , We as Developer don't need to maintain the lifecycle of Object (in spring its called as bean).
So let see the Example of IOC in Spring-
1)Create the Java maven project in eclipse.
2) If you have created simple java project using maven , it will have below structure.
3) As per my Project you can create one package (as in my com.vp.spring)
and see the below code
4) By Watching above program you can see all the Code.
If you want to understand about IOC in depth , Read from Official Documents
Download full Project from here
So What is Inversion Of Control (important Point for Interview as well ) - So basically IOC is as words suggest the responsibility of Creating and Maitaining the lifecycle of Object is of Spring Container only , We as Developer don't need to maintain the lifecycle of Object (in spring its called as bean).
So let see the Example of IOC in Spring-
1)Create the Java maven project in eclipse.
2) If you have created simple java project using maven , it will have below structure.
3) As per my Project you can create one package (as in my com.vp.spring)
and see the below code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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 class="com.vp.spring.Organization"></bean> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vp.spring; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import org.springframework.context.support.FileSystemXmlApplicationContext; | |
public class IOCApp1 { | |
public static void main(String[] args) { | |
// load the application context | |
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-cp.xml"); | |
Organization or =ctx.getBean("com.vp.spring.Organization",Organization.class); | |
System.out.println(or.getSlogan()); | |
((FileSystemXmlApplicationContext)ctx).close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vp.spring; | |
public class Organization { | |
private String slogan ="We Deliver the best"; | |
public String getSlogan(){ | |
return slogan; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.vp.spring</groupId> | |
<artifactId>spring-ioc-demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>spring-ioc-demo</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
<version>4.3.11.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>4.3.11.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-beans</artifactId> | |
<version>4.3.11.RELEASE</version> | |
</dependency> | |
</dependencies> | |
</project> |
4) By Watching above program you can see all the Code.
If you want to understand about IOC in depth , Read from Official Documents
Download full Project from here
If you have any problem , leave me a comment will help you in understand.
Thanks for reading
Noeik