A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Spring ApplicationContext

Before, going to start the steps download and add jars of spring core and apache commons jar in IDE

Spring ApplicationContext example in 3 simple steps:

 
  

Step 1.

package com.techieuncle.springtut; public class Driver { private String driverName; private String vendorName; public String getDriverName() { return driverName; } public void setDriverName(String driverName) { this.driverName = driverName; } public String getVendorName() { return vendorName; } public void setVendorName(String vendorName) { this.vendorName = vendorName; } @Override public String toString() { return "Driver [driverName=" + driverName + ", vendorName=" + vendorName + "]"; } public void displayDriverInfo() { System.out.println("The information about driver is:"+driverName +"@"+vendorName); } }

Step 2.

create bean id :
bean id="driverData" class="com.techieuncle.springtut.Driver"
add two property tag:
property name="driverName" value="OracleConnector"
property name="vendorName" value="OracleVAendor"

Step 3.

 
  

Test Application by main

package com.techieuncle.springtut; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { public static void main(String[] args) { ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext.xml"); Driver d=(Driver) con.getBean("driverData"); d.displayDriverInfo(); } }

Sample Output:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. The information about driver is:OracleConnector@OracleVAendor