Home arrow Java & J2EE arrow Page 3 - Introducing the Spring Framework

Mapping the Beans continued - Java

In this discussion, I will be focusing on the Spring Framework. The first section will be about the whys and wherefores of the Spring Framework. In the second and third sections, I will explain how to use the Spring Framework to implement business logic. In the fourth and fifth sections, I will develop an application that uses the Spring Framework.

TABLE OF CONTENTS:
  1. Introducing the Spring Framework
  2. Using Spring Framework, Step by Step
  3. Mapping the Beans continued
  4. Spring Framework in the Real World
  5. Beans and Client
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 113
March 05, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Property is the child tag of the bean tag. It is used to provide the details of the property of the bean that needs to be accessed or populated. Property is the member variable of the bean that has been mapped using a bean tag. It is done using the name attribute of the property tag. To access the greeting property of the SimpleInterest bean, the property tag will be:


<beans>

<bean id=”Hello” class=”org.me.Hello”>

<property name=”greeting”>

:

</property>

</bean>

<beans>


The value tag is the child of property tag. The value to be given to the property is passed as the child node of the value tag. To pass Good Morning to the greeting property as the value, the code will be:


<beans>

<bean id=”Hello” class=”org.me.Hello”>

<property name=”greeting”>

<value>Good Morning<value>

</property>

</bean>

<beans>


Next, we will see what is required to implement the client.

Developing the Client: Clients need to do the following to access the Spring Framework service:

  • Instantiate the configuration file as a resource
  • Instantiate an implementation of BeanFactory
  • Call the business method

Here are the details.

The first step is to instantiate the configuration file as a resource. To access the business methods, first the configuration file needs to be passed to the framework. Spring accepts the configuration file as an instance of the Resource interface’s implementation class. The most commonly used implementation class is ClassPathResource. It is instantiated using the name of the configuration file. For example, to create an instance of ClasspathResource with the filename beans.xml and assign it to the reference of the Resource interface, the statement will be:

Resource resource=new ClassPathResource(“beans.xml”);

Now we are ready for the second step. To access the beans populated with values, the second step is to get a reference for the BeanFactory interface. The reference would contain the instance of an implementation of the BeanFactory interface. The commonly used implementation is XmlBeanFactory. To get an instance of XmlBeanFactory, the reference of the Resource interface has to be passed to the constructor of XmlBeanFactory. For example, to get an instance of XmlBeanFactory and assign it to a reference of BeanFactory, the statement will be:

BeanFactory factory=new XmlBeanFactory(resource);

Finally, to call the business methods, the instance of bean needs to be retrieved using the getBean() method of BeanFactory. The method return Object instance needs to be typecast into the POJO class. Once that is done, the business objects can be called. In code, it will be:


Hello hello=( Hello)factory.getBean(“Hello”);

System.out.println(hello.sayHello());


That completes the steps for using the Spring Framework. Next, let us look at a real world example.



 
 
>>> More Java & J2EE Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: