SpringPresentation

Embed Size (px)

Citation preview

  • 8/7/2019 SpringPresentation

    1/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 containerand framework.

    Lightweight Spring is lightweight in terms of both size andoverhead. Thebulk of the Spring Framework can be distributed in a single JARfile that

    weighs in at just over 2.5 MB.Dependency Injection Spring promotes loose couplingthrough a techniqueknown as dependency injection (DI). You can think of DI asJNDI in reverse instead of an object looking up dependenciesfrom a container, the container gives the dependencies to theobject at instantiation without waiting to be asked.Aspect-oriented Spring comes with rich support foraspect-oriented programming (AOP) that enables cohesivedevelopment by separating application business logic fromsystem services (such as auditing and transactionmanagement). Application objects do what theyre supposedto do perform business logic and nothing more.

  • 8/7/2019 SpringPresentation

    2/9

    Architectural benefits of Spring

    * Spring can effectively organize your middle tier objects. If you use onlyStruts or other frameworks geared to particular J2EE APIs. And Spring's

    configuration management services can be used in any architectural layer, inwhatever runtime environment.

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

    * Spring eliminates the need to use a variety of custom properties fileformats, 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 itsAPIs as possible. Most business objects in Spring applications have nodependency on Spring.

  • 8/7/2019 SpringPresentation

    3/9

    *Spring provides an alternative to EJB that's appropriate for many applications.For example, Spring can use AOP to deliver declarative transaction managementwithout using an EJB container.

    The Spring Framework is composed of several well-defined modules built on top of the corecontainer. This modularity makes it possible to use as much or as little of the Spring Frameworkas is needed in a particular application.

  • 8/7/2019 SpringPresentation

    4/9

    The concept of DI transcends Spring. Thus, you can accomplish DIwithout 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.}

    `

  • 8/7/2019 SpringPresentation

    5/9

    Then injecting a different transport is a mere matter of calling a different setter method asfollows:

    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 = newClassPathXmlApplicationContext("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.}

  • 8/7/2019 SpringPresentation

    6/9

    01.02.

    06.07. 08.09.10. 11. 12. 13.14.

    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.}

  • 8/7/2019 SpringPresentation

    7/9

    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.

  • 8/7/2019 SpringPresentation

    8/9

    < property name ="driverClassName"

    value ="com.mysql.jdbc.Driver" >

    < property name ="url"value ="jdbc:mysql://localhost:3306/hollywoodcare" >

    < property name ="username" value ="root" >< property name ="password" value ="root" >

    < property name ="dataSource" >

    com.hollywoodcare.eps.Stripboard com.hollywoodcare.eps.State com.hollywoodcare.eps.ExhibitG com.hollywoodcare.eps.Document com.hollywoodcare.eps.Strip com.hollywoodcare.eps.ScriptElement

    < property name ="sessionFactory" >

    < bean id = "StripboardDAO" class = "com.hollywoodcare.eps.StripboardDAO" >

    < property name ="sessionFactory" >

    ApplicationContex.xml

  • 8/7/2019 SpringPresentation

    9/9

    contextConfigLocation /WEB-INF/applicationContext.xml

    web.xml