You are on page 1of 9

What is Spring ?

Spring does many things, but when you strip it down to its base parts,
Spring is a lightweight dependency injection and aspect-oriented container
and framework.

Lightweight—Spring is lightweight in terms of both size and


overhead. The
bulk of the Spring Framework can be distributed in a single JAR
file that
weighs in at just over 2.5 MB.
Dependency Injection—Spring promotes loose coupling
through a technique
known as dependency injection (DI). You can think of DI as
JNDI in reverse—instead of an object looking up dependencies
from a container, the container gives the dependencies to the
object at instantiation
Aspect-oriented— without
Spring waiting
comes withtorich
be asked.
support for
aspect-oriented programming (AOP) that enables cohesive
development by separating application business logic from
system services (such as auditing and transaction
management). Application objects do what they’re supposed
to do—perform business logic—and nothing more.
Architectural benefits of Spring

* Spring can effectively organize your middle tier objects. If you use only
Struts or other frameworks geared to particular J2EE APIs. And Spring's
configuration management services can be used in any architectural layer, in
whatever runtime environment.

* Spring can eliminate the proliferation of Singletons seen on many projects.

* Spring eliminates the need to use a variety of custom properties file


formats, by handling configuration in a consistent way throughout applications
and projects. With Spring you simply look at the class's JavaBean properties or
constructor arguments.

* Spring facilitates good programming practice by reducing the cost of


programming to interfaces, rather than classes, almost to zero.

* Spring is designed so that applications built with it depend on as few of its


APIs as possible. Most business objects in Spring applications have no
dependency on Spring.
*Spring provides an alternative to EJB that's appropriate for many applications.
For example, Spring can use AOP to deliver declarative transaction management
without using an EJB container.

The Spring Framework is composed of several well-defined modules built on top of the core
container. This modularity makes it possible to use as much or as little of the Spring Framework
as is needed in a particular application.
The concept of DI transcends Spring. Thus, you can accomplish DI
without Spring as follows `

01.package com.arcmind.springquickstart;
02.
03.import java.math.BigDecimal;
04.
05.public class AtmMain {
07. public void main (String[] args) {
08. AutomatedTellerMachine atm = new AutomatedTellerMachineImpl();
09. ATMTransport transport = new SoapAtmTransport();
10. /* Inject the transport. */
11. ((AutomatedTellerMachineImpl)atm).setTransport(transport);
12.
13. atm.withdraw(new BigDecimal("10.00"));
14.
15. atm.deposit(new BigDecimal("100.00"));
16. }
18.}
Then injecting a different transport is a mere matter of calling a different setter method as
follows:

Injecting a different dependency

1.ATMTransport transport = new SimulationAtmTransport();


2.((AutomatedTellerMachineImpl)atm).setTransport(transport);

To use Spring to inject a dependency you could do the following:


package com.arcmind.springquickstart;
02.
03.import java.math.BigDecimal;
04.
05.import org.springframework.context.ApplicationContext;
06.import org.springframework.context.support.ClassPathXmlApplicationContext;
07.
08.public class AtmMain {
09.
10. public static void main (String[] args) {
11. ApplicationContext appContext = new
ClassPathXmlApplicationContext("classpath:./spring/applicationContext.xml");
12. AutomatedTellerMachine atm = (AutomatedTellerMachine) appContext.getBean("atm");
13.
14. atm.withdraw(new BigDecimal("10.00"));
15.
16. atm.deposit(new BigDecimal("100.00"));
17. }
18.
19.}
01.<?xml version="1.0" encoding="UTF-8"?>
02.<beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xsi:schemaLocation="
05. http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
06.
07. <bean id="atmTransport" class="com.arcmind.springquickstart.SoapAtmTransport" />
08.
09.
10. <bean id="atm" class="com.arcmind.springquickstart.AutomatedTellerMachineImpl">
11. <property name="transport" ref="atmTransport" />
12. </bean>
13.
14.</beans>

package com.arcmind.springquickstart;
2.
3.import java.math.BigDecimal;
4.
5.public interface AutomatedTellerMachine {
6. void deposit(BigDecimal bd);
7. void withdraw(BigDecimal bd);
8.}
package com.arcmind.springquickstart;
2.
3.public interface ATMTransport {
4. void communicateWithBank(byte [] datapacket);
5.}

package com.arcmind.springquickstart;
02.
03.import java.math.BigDecimal;
04.
05.public class AutomatedTellerMachineImpl implements AutomatedTellerMachine{
06.
07. private ATMTransport transport;
08.
09. public void deposit(BigDecimal bd) {
10. ...
11. transport.communicateWithBank(...);
12. }
13.
14. public void withdraw(BigDecimal bd) {
15. ...
16. transport.communicateWithBank(...);
17. }
18.
19. public void setTransport(ATMTransport transport) {
20. this.transport = transport;
21. }
22.
ApplicationContex.xml
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/hollywoodcare">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="annotatedClasses">
<list>
<value>com.hollywoodcare.eps.Stripboard</value>
<value>com.hollywoodcare.eps.State</value>
<value>com.hollywoodcare.eps.ExhibitG</value>
<value>com.hollywoodcare.eps.Document</value>
<value>com.hollywoodcare.eps.Strip</value>
<value>com.hollywoodcare.eps.ScriptElement</value>
</list>
</property>
</bean>
<bean id="StripboardReportDAO"
class="com.hollywoodcare.eps.StripboardReportDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="StripboardDAO" class="com.hollywoodcare.eps.StripboardDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

You might also like