Beruflich Dokumente
Kultur Dokumente
Usa Sammapun
Motivation
Business needs software system to run their business Flexibility Extensibility Reusability Java libraries provide only infrastructures to develop system Most business software has very similar high-level architecture Three-tier architecture
01418471 Software Design and Development .
2
Three-tier Architecture
Middle tier
Spring Framework
Framework : Libraries of classes Facilitating database connectivity, transaction management, faulttolerance, modular systems Promote exibility, extensibility, reusability Container : Object manager Create objects (beans) according to your specication Provide interface for accessing these objects These objects are normally composed to build a business solution
01418471 Software Design and Development .
4
JavaBeans
Classes conforming to a specic standard JavaBeans (java.beans.*) Intra-process In Java SE Enterprise JavaBeans (EJBs) (javax.ejb.*) Inter-process In Java EE
01418471 Software Design and Development .
5
Spring Framework
Reduce dependencies among objects Promote using interface rather than implementation Inversion of Control (IoC) Lightweight Most classes can be independent of Spring Separation of responsibility Aspect-oriented programming
01418471 Software Design and Development .
7
Source: http://martinfowler.com/articles/injection.html 8
Source: http://martinfowler.com/articles/injection.html 9
Source: http://martinfowler.com/articles/injection.html 10
MovieLister dependencies
MovieLister has a following dependency
If used by others with different a MovieLister implementation This dependency should not be hardwired within the program Depend only on the interface but still need to get to the object ?
01418471 Software Design and Development .
Source: http://martinfowler.com/articles/injection.html 11
MovieLister example
Or even if we push the object instantiation up to the caller / factory the caller / factory still depends on a MovieFinder implementation
class MovieLister ... private MovieFinder finder; public MovieLister(MovieFinder finder) { this.finder = finder; } ... class MovieEngine ... public MovieLister(MovieFinder finder) { MovieFinderfinder = new CSVMovieFinder("movies1.txt"); MovieLister lister = new MovieLister(finder)
Source: http://martinfowler.com/articles/injection.html 12
Dependency Injection
Use a separate object, an assembler, to create desired objects
Source: http://martinfowler.com/articles/injection.html 14
Source: http://martinfowler.com/articles/injection.html 15
IoC/DI in Spring
Core : IoC Container Manage object within Spring Separate object creation and object execution Developers describe how to create objects (vis xml cong le) Without creating it yourself (no need for new keywords) To reduce dependency between components Thus, decoupling components
01418471 Software Design and Development .
16
17
18
19
<bean id="csv-finder" class="springlesson.movie.CSVMovieFinder"> <constructor-arg value="movies1.txt"/> </bean> <bean id="lister" class="springlesson.movie.MovieLister"> <constructor-arg ref="csv-finder"/> </bean> </beans>
20
<bean id="csv-finder" class="springlesson.movie.CSVMovieFinder"> <constructor-arg value="movies1.txt"/> </bean> <bean id="lister" class="springlesson.movie.MovieLister"> <constructor-arg ref="csv-finder"/> </bean> </beans>
20
<bean id="csv-finder" class="springlesson.movie.CSVMovieFinder"> <constructor-arg value="movies1.txt"/> </bean> <bean id="lister" class="springlesson.movie.MovieLister"> <constructor-arg ref="csv-finder"/> </bean> </beans>
20
<bean id="csv-finder" class="springlesson.movie.CSVMovieFinder"> <constructor-arg value="movies1.txt"/> </bean> <bean id="lister" class="springlesson.movie.MovieLister"> <constructor-arg ref="csv-finder"/> </bean> </beans>
20
<bean id="csv-finder" class="springlesson.movie.CSVMovieFinder"> <constructor-arg value="movies1.txt"/> </bean> <bean id="lister" class="springlesson.movie.MovieLister"> <constructor-arg ref="csv-finder"/> </bean> </beans>
20
<bean id="csv-finder" class="springlesson.movie.CSVMovieFinder"> <constructor-arg value="movies1.txt"/> </bean> <bean id="lister" class="springlesson.movie.MovieLister"> <constructor-arg ref="csv-finder"/> </bean> </beans>
20
21
22
23
Spring Framework
Bean Factory
Very generic implementation of the Factory design pattern Used to create any beans by giving its name Core of IoC and dependency injection (DI) Similar concepts Application Context Just like bean factory but has more features
01418471 Software Design and Development .
26
27
28
29
30
31
32
34
35
36
37
} }
38
39