Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Ru Shen Department of Computer Science UAlbany, 2008

Similar presentations


Presentation on theme: "By Ru Shen Department of Computer Science UAlbany, 2008"— Presentation transcript:

1 By Ru Shen Department of Computer Science UAlbany, 2008
Spring Framework By Ru Shen Department of Computer Science UAlbany, 2008

2 Outline of the presentation
History of Spring The framework What is it good for? Spring Inversion of Control Spring Aspect Oriented Programming My experience with Spring AOP Installing Spring References Small assignment

3 A little bit of history Who Rod Johnson (Sydney University) When
Spring was started in Feb 2003. What Spring is an open source framework for building enterprise Java applications.

4 Overview of Spring framework
Picture excerpted from Manning – Spring in Action by Craig Walls and Ryan Breidenbach

5 Overview of Spring framework continued
The core container is the fundamental layer of the framework that provides Inversion of Control functionality. Application context module extends the core modules by supplying many enterprise services such as JNDI access, remote access, multi-language support and scheduling. AOP module provides rich support for aspect-oriented programming.

6 Overview of framework contintued
JDBC layer simplifies the process of database connection. ORM module provides compatibility to existing framework such as Hibernate, JDO and TopLink. Web module provides a context for web-based applications. Spring comes with a full-featured MVC framework for building web applications.

7 What is Spring good for? Allows you to build applications with Plain Old Java Objects (POJOs) containing only the business logic. vs EJB The famous “Inversion of Control”. Compatible with various ORM technology, such as TopLink and Hibernate. Provides a testing framework that you can perform unit testing on. Supports Aspect Oriented Programming. Supports Model-View-Controller framework.

8 Spring IoC IoC stands for Inversion of Control
Hollywood Principle: “Don’t call me, I’ll call you.” In a nutshell … inversion of control is about: the responsibility of coordinating collaboration between dependent objects is transferred away from the objects themselves. – Spring in Action

9 Traditional approach In order to use object B, object A has to instantiate from Class B. In order to use object C, object B has to instantiate from Class C. Object A Object B Class B Object C Class C

10 Problem with traditional approach
Classes are tightly coupled Hard to test: A← B, B← C Hard to reuse Hard to understand: cannot see the main flow Hard to maintain: fixing one bug may result creating more new bugs Utilizing interface will not solve the problem

11 Revised approach changes … <beans> <bean id=A class=A>
<property name=b> <ref bean=B> </property> </bean> <bean id=B class=B> <property name=c> <ref bean=C> <bean id=C class=C/> </beans> Class A{ private BGroup b; Public void setBGroup(BGroup b){ this.b = b; } Interface BGroup Class B{ private CGroup c; Public void setCGroup(CGroup c){ this.c = c; } Class B’ Class B’’ Interface CGroup Class C Class C’ Class C’’

12 Components of Spring IoC
IoC is practiced using Dependency Injection Two forms of DI: Setter Injection and Constructor Injection Code are decoupled with interface Spring IoC Container is the platform Bean factory produce beans Configuration file defines beans and the main flow

13 Demonstrate Spring IoC
Bean factory is created with a single line of code Beans are obtained from the bean factory Picture excerpted from Manning – Spring in Action by Craig Walls and Ryan Breidenbach

14 Demonstrate Spring IoC continued
Picture excerpted from Manning – Spring in Action by Craig Walls and Ryan Breidenbach

15 Demonstrate Spring IoC continued
Things to notice: Class implements interface A setter method Container calls the setter What’s the impact: Any class that implements “Knight” can be called at run time Knight is GIVEN a quest rather than GETTING a quest Picture excerpted from Manning – Spring in Action by Craig Walls and Ryan Breidenbach

16 Demonstrate Spring IoC continued
An example of Constructor Injection Picture excerpted from Manning – Spring in Action by Craig Walls and Ryan Breidenbach

17 Summarize Spring IoC Decoupling with interface
Configuration file defines the rules Objects are given rather than obtained More options in the configuration file Inheritance between the beans Singleton or non-singleton Abstract beans The Spring container glues everything together

18 Spring AOP Aspect-Oriented Programming AOP complements OOP
Decompose programs into aspects (concerns) An aspect crosscuts multiple programs Common aspects: Exception handling Transaction management Logging Security checking

19 Aspect-Oriented Programming
Class A Class B Class C Aspect 1 (logging) Aspect 2 (security check) Classes handle functional requirements Use cases Functions Aspects handle non-functional requirements Security Logging One aspect usually impacts multiple classes. It’s tedious to implement the aspect in each class.

20 AOP concepts Aspect Join point – A method invocation
Advice – Action to take at a join point Point cut – A set of join points Target – Object containing the join point AOP proxy and advisor – Objects for weaving the above components together

21 Type of Advices in AOP Before advice After advice Around advice
Executed before a joint point. After advice Executed after a joint point completes. Around advice Most powerful one. Both before and after. Throws advice Before throwing an exception After throwing an exception

22 Weave the components together
Advice Point cut Advisor + = + A method of Target = Deploy it to the Spring container! Proxy

23 My experience with Spring AOP
The purpose: exception handling Background: Java Enterprise Application on EJB Backend Weblogic server Front end Swing Uniformed method for exception handling – prompt the formatted error messages to users

24 AOP continued … Vehicle management Driver management
Add vehicle / driver Remove vehicle / driver Update vehicle / driver Search vehicle / driver Management module Vehicle management Driver management Scheduling module Dispatch module Report module

25 AOP continued Without using AOP, each exception has to be handled individually, even though they are handled in the same way. With AOP, we don’t handle exceptions in individual classes. We just throw them. One advice is sufficient to handle a group of exceptions.

26 AOP continued Spring embedded AOP objects ThrowsAdvice
RegexpMethodPointcutAdvisor ProxyFactoryBean MyVehicleProxy ThrowsAdvice Pointcut Regular expression pattern Vehicle management bean MyAdvice ProxyFactoryBean RegexpMethodPointcutAdvisor MyAdvisor

27 Summarize Spring AOP Embedded objects to facilitate AOP implementation, runtime Works for Spring beans only – vehicle management bean has to be deployed in the container More things to explore with beforeAdvice, afterAdvice and aroundAdvice

28 Install Spring 1. Download Spring framework from 2. Include the downloaded spring.jar in your $CLASSPATH. Maybe you need to download apache commons logging package: commons-logging-1.1.jar. It is used internally by Spring. 3. Write your first configuration file. 4. Create bean factory by specifying the location of the configuration file. 5. Refer each class through their bean name 6. Start writing your Java classes.

29 Reference Introduction by Rod Johnson on The Server Side
Manning – Spring in Action Spring reference documentation

30 A small assignment Using Spring, write a simple program that prints the configuration of different brands of cars. Define Car as an interface. Try printing the configuration of different brands of cars by writing different implementation of Car. Modify the configuration file at runtime to switch between the cars every time and see the output.


Download ppt "By Ru Shen Department of Computer Science UAlbany, 2008"

Similar presentations


Ads by Google