Introducing the Spring Framework - Beans and Client (
Page 5 of 5 )
Next is the beans.xml file. It contains configuration information for the POJO and its properties.
<beans>
<bean id=”SimpleInterestBean”
class=”org.me.SimpleInterestCalculatorBean”>
<property name=”principle”>
<value>10000.00<value>
</property>
<property name=”years”>
<value>10.00<value>
</property>
<property name=”rate”>
<value>9.50<value>
</property>
</bean>
<beans>
Last is the code for Client.java. It first loads the configuration, then gets a reference for BeanFactory and retrieves the instance of SimpleInterestCalculatorBean. After that, it calls the calculate method to get the calculated simple interest. Here is the code:
import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
public class Client
{
public static void main(String args[]) throws Exception
{
try
{
System.out.println("please Wait.");
Resource res = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(res);
SimpleInterestCalculatorBean interest=
(SimpleInterestCalculatorBean)factory.getBean(“SimpleInterestBean);
System.out.println(interest.getInterest());
}
catch(Exception e1)
{ System.out.println(""+e1); }
}
}
That completes the introduction to Spring Framework. What has been described here is just the tip of the iceberg. In the future, I will be discussing each of the components of Spring Framework in depth. Till then…