Magazine
 
Spring
 
 

We will get the output as follows:

please Wait.Jul 17, 2007 5:40:24 PM
org.springframework.core.CollectionFactory <clinit>INFO:
DK 1.4+ collections availableJul 17, 2007 5:40:24 PM
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitionsINFO: Loading XML bean definitions from
class path resource [hello.xml]Good Morning!...AMIT

Thus we have run our first spring program. Here helloimpl
implements the hello interface. Although it is not necessary to
hide the implementation behind an interface, it is recommended
as a way to seperate implementation from interface.

helloimpl class has a single property greeting. This property
can be set in two different ways: by property’s setter method
or by the constructor. The XML file hello.xml declares the
instance of helloimpl.java in the spring container and configures
its property greeting with the value ‘Good Morning!...’

The root of the hello.xml file is the <beans> element, which is
the root element of any Spring configuration file. The <bean>
element is used to tell the spring container about the class
and how it should be configured. Here, the id attribute takes
the interface name and the class attribute specifies the
bean’s fully qualified class name.

Within the <bean> element, the <property> element is used
to set the property, in this case the greeting property. By
using <property>, we’re telling the Spring container to call
setGreeting() while setting the property. The value of the
greeting is defined within the <value> element. Here we have
given ‘Good Morning!...’ as the value.

The container instantiates the ‘helloimpl’ based on the XML
definition as,
helloimpl hello = new helloimpl();
helloimpl.setGreeting(“Good Morning!...”);

Similarly, greeting property may be set through the single
argument constructor of ‘helloimpl’ as:

<bean id=”hello” class=”helloimpl”> <constructor-arg>
<value>Good Morning!...</value>
</constructor-arg> </bean>

The container instantiates the ‘helloimpl’ based on the XML
definition as:

helloimpl hello = new helloimpl(“Good Morning...”);
In the client program, ‘BeanFactory’ class is used which is a
spring container. Then the ‘getBean()’ method is called to get
the reference to the ‘hello’. With this reference, sayhello()
method is called.

   
August 2007 | Java Jazz Up |24
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40           Download PDF