Spring - Inversion of Control
Sample Program to create object and call method without using 'Spring Framework'
package com.ah.ioc;
public class Test {
public static void main(String[] args) {
SamsungMobile sm = new SamsungMobile();
sm.display();
}
}
package com.ah.ioc;
public class SamsungMobile implements Mobile{
public SamsungMobile(){
System.out.println("Constructor of SamsungMobile class");
}
public void display(){
System.out.println("display() method in SamsungMobile class");
}
}
Maven Dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.16</version>
</dependency>
Sample Program to create object and call method using 'Spring Framework'
package com.ah.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_ioc.xml");
SamsungMobile samsungMobile = context.getBean("sm", SamsungMobile.class);
samsungMobile.display();
}
}
package com.ah.ioc;
public class SamsungMobile implements Mobile{
public SamsungMobile(){
System.out.println("Constructor of SamsungMobile class");
}
public void display(){
System.out.println("display() method in SamsungMobile class");
}
}
beans_ioc.xml
<?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 id="sm" class="com.ah.ioc.SamsungMobile"/>
</beans>
Sample Program using interface 'Spring Framework'
package com.ah.ioc;
public interface Mobile {
public void display();
}
package com.ah.ioc;
public class SamsungMobile implements Mobile{
public SamsungMobile(){
System.out.println("Constructor of SamsungMobile class");
}
public void display(){
System.out.println("display() method in SamsungMobile class");
}
}
package com.ah.ioc;
public class NokiaMobile implements Mobile{
public NokiaMobile(){
System.out.println("Constructor of NokiaMobile class");
}
public void display(){
System.out.println("display() method in NokiaMobile class");
}
}
package com.ah.ioc;
public class ABCMobile implements Mobile{
public ABCMobile(){
System.out.println("Constructor of ABCMobile class");
}
public void display(){
System.out.println("display() method in ABCMobile class");
}
}
package com.ah.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_ioc.xml");
Mobile mobile = context.getBean("mobile", Mobile.class);
mobile.display();
}
}
beans_ioc.xml
<?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 id="mobile" class="com.ah.ioc.ABCMobile"/>
</beans>
Watch Demo
No comments:
Post a Comment