Create a implementation class ImplCode.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class ImplCode { public static void main(String[] args) { ApplicationContext appContext=new FileSystemXmlApplicationContext("AppContextBeanFile.xml"); Fruit fruit=appContext.getBean("fruit", Fruit.class); Vegetable veg=(Vegetable) appContext.getBean("veg"); System.out.println(fruit.whoareyou()); System.out.println(veg.whoareyou()); } } |
Create a bean class Vegetable.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | package rashiddemo; public class Vegetable { public String whoareyou() { String intro="I am vegetable!!"; return intro; } } |
Create another bean class Fruit.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | package rashiddemo; public class Fruit { public String whoareyou() { String intro="I am fruit!"; return intro; } } |
Create a AppContextBeanFile.xml and add your bean classes using bean tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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="fruit" class="rashiddemo.Fruit"></bean> <bean id="veg" class="rashiddemo.Vegitable"></bean> </beans> |