Professional Documents
Culture Documents
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.
* 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.
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:
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>