The Freedom to Achieve - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

The Freedom to Achieve

Description:

This will tell Spring to instantiate a ThirstyPerson object, and ... Let's make our objects a little cleaner. PlatinumSolutions, Inc. 15. Further Decoupling ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 32
Provided by: adamr8
Category:

less

Transcript and Presenter's Notes

Title: The Freedom to Achieve


1
The Freedom to Achieve
  • Introduction to Spring
  • Platinum Solutions
  • All Hands Meeting
  • 2/18/2007
  • Navid Norouzi

2
What is Spring?
  • An open source J2EE Framework
  • Spring is made up of different components
  • Spring Core
  • Spring Web
  • Spring ORM
  • Spring DAO
  • Spring AOP
  • Other

3
Spring Components
4
Why Use It?
  • Enables better use of POJOs (Plain Old Java
    Objects)
  • Promotes Decoupling, Reusability, Testing
  • Built to integrate with other Popular Open Source
    Projects (e.g. Hibernate)

5
Inversion of Control (IoC)?
  • Inversion of Control is a programming principle
  • Also known as Dependency Injection
  • IoC Promotes Decoupling, Reusability, Testing (as
    mentioned before)

6
Non-IoC Example
interface Cup drink() isReusable()
class PaperCup implements Cup private String
liquid Public PaperCup (liquid) this.liquid
liquid drink() isReusable() return
false
7
Non-IoC Example Continued
class ThirstyPerson Private Cup myCup public
ThirstyPerson() myCup new PaperCup(WATER)
  • Weve coupled our Thirsty Person to drink only
    water if he/she wants to use their Cup

8
IoC Version
  • We can set our Thirsty Person to drink other
    liquids besides water (de-coupling)
  • First, we need to make a change to our
    ThirstyPerson class
  • We will allow the liquid to be set via the
    constructor and a setter method

9
IoC Version Continued
class ThirstyPerson Private Cup myCup //
constructor injection public ThirstyPerson(LIQUID)
myCup new PaperCup(LIQUID)
10
IoC Version Continued
  • Next we wire the liquid we want with the
    Thirsty Person in a configuration file outside of
    the code
  • A XML file serves as the configuration file
  • Spring will process the configuration file

11
Spring Beans Definition XML
  • This will tell Spring to instantiate a
    ThirstyPerson object with Apple Juice as the
    liquid passed to the constructor

12
IoC Version Continued
class ThirstyPerson Private Cup myCup public
ThirstyPerson() // empty constructor //
setter injection public setLiquid(LIQUID)
myCup new PaperCup(LIQUID)
13
Spring Beans Definition XML
  • This will tell Spring to instantiate a
    ThirstyPerson object, and then call the setter
    method of the liquid property with the value of
    Coffee

14
IoC Version Continued
  • We used an XML file to instantiate our object
    and decoupled our Thirsty Person from being
    forced to start with water
  • Note that Thirsty Person does not really have a
    LIQUID property, but a CUP property and that
    Thirsty Person is coupled with a Paper Cup no
    matter what
  • Lets make our objects a little cleaner

15
Further Decoupling
class ThirstyPerson Private Cup myCup // empty
constructor public ThirstyPerson() //
constructor injection public ThirstyPerson(CUP)
myCup CUP // setter injection public
setCup(CUP) myCup CUP
16
Further Decoupling Continued
interface Cup drink() setLiquid() getLiquid() isRe
usable() setReusable()
class AnyCup implements Cup private String
liquid private boolean reusable // empty
constructor Public AnyCup () drink() setLiquid(
) getLiquid() isReusable() setReusable()
17
Further Decoupling Continued
  • Now ThirstyPerson class only deals with CUPs, and
    our CUP class is now generic
  • We need to change our Spring XML file since we
    changed our Class
  • We can now create CUP beans and pass them into
    our ThirstyPerson

18
Further Decoupling Continued

19
Further Decoupling Continued

20
Introduction to Spring
  • If your properties are REQUIRED to be set, use
    Constructor Injection, to force the properties to
    get set during instantiation
  • We have decoupled (ThirstyPerson) and reused
    (AnyCup)
  • Most likely, we should further de-couple our Cups
    from their fixed liquid contents
  • We can now make changes to the choice of drink,
    outside of our compiled code

21
Spring Promotes Testing
  • Now well take a look at a simple JUnit Test Case
  • Our test will create an Application from our
    configuration file

22
Spring Promotes Testing
class CeramicCupTest extends TestCase Private
AnyCup anyCeramicCup setUp() // create the
application from our configuration file // this
will instantiate our beans ApplicationContext
context new ClassPathXmlApplicationContext(Ou
rSpringXMLFile.xml) // ceramicCup is
defined as a bean in our configuration
file anyCeramicCup (AnyCup) context.getBean(ce
ramiCup) testCeramiCup() assertTrue(anyCera
micCup.isReusable)
23
Spring Promotes Testing
  • Can more easily test your Application using Stubs
    and Mocks
  • Stubs
  • Provide canned answers to calls made during a
    test replacing a method with code that returns
    a specific result
  • Mock Objects
  • Are preprogrammed with expectations so you can
    assert if the expectation was met (was a method
    called?)

24
Introduction to Spring
  • In Summary, Spring, due to its Inversion of
    Control nature, promotes
  • Decoupling
  • Reusability
  • Testing
  • Programming to Interfaces
  • Centralized Configuration
  • Other

25
Spring Application Context Lifecycle
  • Initialization
  • Application resources (beans) are created
  • Use
  • Used by application services
  • Destruction
  • Releases resources for garbage collection

26
Bean properties
  • Beans also have various properties
  • Scope
  • Session a new instance is created once per
    session
  • Request a new instance is created once per
    request
  • Other
  • Other properties, such as Lazy Instantiation
  • Will not be created until referenced
  • Beans can have inheritance!
  • Inner Beans
  • An anonymous bean only available to its parent
    bean
  • Other

27
Spring AOP?
  • AOP
  • Stands for Aspect Oriented Programming
  • Object Oriented Programming
  • Concentrates on Classes
  • Aspect Oriented Programming
  • Concentrates on Aspects

28
Spring AOP Continued
  • AOP works with the Compiler
  • Modularizes concerns in your Application,
    concerns that cross-cut across your application
  • Logging
  • Tracing
  • Transactions
  • Security
  • Caching
  • Error Handling
  • Custom Business Rules
  • Other
  • Can provide a lot of flexibility and power, can
    also be abused
  • Example Using AOP, log each time a specific
    variable is changed via its setter method

29
Other Spring Components
  • Spring Data Access
  • Spring provides libraries that support and
    simplify writing data access code
  • Supports JDBC, Hibernate, iBatis, JPA, other
  • Spring Web
  • Spring MVC
  • Request driven framework (Similar to Struts)
  • Requests are routed through Controllers by the
    Dispatcher Servlet
  • The Controllers determine how to render the
    results
  • Spring Web Flow
  • State and Flow management
  • Wizards, Step-by-Step navigation
  • Adds a scope Page, Request, Flow, Session,
    Application

30
Other Spring Components Continued
  • Spring Security
  • Formerly known as ACEGI Security
  • Where did the name come from A B C D E F G H I
  • Spring Web Services
  • Spring Remoting
  • In place of direct RMI
  • Spring JMS
  • Simplified Messaging / Producer and Consumer
    cases
  • Other Components

31
Introduction to Spring
  • Thank you.
Write a Comment
User Comments (0)
About PowerShow.com