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…
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |