SlideShare ist ein Scribd-Unternehmen logo
1 von 59
Spring Framework Jens Rühmkorf  Tech Talk, DLR Köln-Porz, 17. März 2009
Inhalt ,[object Object],[object Object],[object Object],[object Object]
Überblick und Einführung
Spring Framework Geschichte ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Spring Framework Anforderungen an die Systemumgebung ,[object Object],[object Object],[object Object],[object Object]
Übersicht des Spring Frameworks http://static.springframework.org/spring/docs/2.5.x/reference/
Spring Hello (1) Hello World!  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Spring Hello (2) Main ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Spring Hello (3) The Glue ,[object Object],[object Object],[object Object],[object Object],[object Object]
Spring Hello (4) Ausgabe ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Grundlegende Prinzipien
„ Separation of Concerns “ (Dijkstra, 1974)
Inversion of Control (1) Don‘t call us, we‘ll call you! ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inversion of Control (2) Verwendung in Frameworks ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inversion of Control (3) Bekannte Beispiele ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inversion of Control (4) Typische Einsatzgebiete ,[object Object],[object Object],[object Object]
Inversion of Control (5) Konzeptuelle Unterscheidung ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Inversion of Control (5) Elementare Begrifflichkeiten ,[object Object],[object Object]
Dependency Lookup (1) Dependency Pull ,[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Dependency Lookup (2) Dependency Pull am Beispiel von OSGi zur Kommunikation mit zentraler „Service Registry“ verwende Service schlage Service nach
Dependency Lookup (3) Contextualized Dependency Lookup (CDL) ,[object Object],[object Object],[object Object],[object Object],public   interface  ManagedComponent { public   void  performLookup(Container container); }
Dependency Lookup (4) Contextualized Dependency Lookup (CDL) ,[object Object],[object Object],[object Object],public   class  ContextualizedDependencyLookup  implements  ManagedComponent { private  Dependency  dep ; public   void  performLookup(Container container) { this . dep  = (Dependency)    container.getDependency( "myDependency" ); } } Aufruf durch Container
Dependency Lookup (5) CDL = Interface Injection = Type 1 IoC ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Dependency Injection (1) Setter Injection (= Type 2 IoC) ,[object Object],[object Object],public   class  SetterInjection { private  Dependency  dep ; public   void  setMyDependency(Dependency dep) { this . dep  = dep; } }
Dependency Injection (2) Constructor Injection (= Type 3 IoC) ,[object Object],[object Object],public   class  ConstructorInjection { private  Dependency  dep ; public  ConstructorInjection(Dependency dep) { this . dep  = dep; } }
Funktionsweise eines DI-Frameworks am Beispiel
Arbeitsweise des Frameworks Beispiel: ein Reportgenerator ,[object Object],[object Object],[object Object]
Reportgenerator (1) Problem: Abhängig von Implementierung ,[object Object],public   class  ReportService { private  ReportGenerator  reportGenerator  =  new     PdfReportGenerator(); public   void  generateAnnualReport( int  year) { String[][] statistics =  null ; //  Erzeuge   Statistiken   fuer   das   Jahr reportGenerator .generate(statistics); } //  Rest   weggelassen }
Reportgenerator (2) Schritt 1: Führe Container ein (UML) ,[object Object]
Reportgenerator (3) Schritt 1: Führe Container ein (cont‘d) public   class  Container { public   static  Container  instance ; private  Map<String, Object>  components ; public  Container() { components  =  new  HashMap<String, Object>(); instance  =  this ; components .put( &quot;reportGenerator&quot; ,  new  PdfReportGenerator()); components .put( &quot;reportService&quot; ,  new  ReportService()); } public  Object getComponent(String id) {  return   components .get(id); } }
Reportgenerator (4) Schritt 2: Verwende Service-Locator Pattern (UML) ,[object Object]
Reportgenerator (5) Schritt 2: Verwende Service-Locator Pattern (cont‘d) public   class  ServiceLocator { private   static  Container  container  = Container. instance ; public   static  ReportGenerator getReportGenerator() { return  (ReportGenerator)    container .getComponent( &quot;reportGenerator&quot; ); } } public   class  ReportService { private  ReportGenerator  reportGenerator  =   ServiceLocator. getReportGenerator (); //  Rest   weggelassen }
Reportgenerator (6) Schritt 3: Realisiere DI via Reflection (UML) ,[object Object],[object Object]
Reportgenerator (7) Schritt 3: Realisiere DI via Reflection (cont‘d) public class  Container {  //  Ausschnitt! private   void  processEntry(String key, String value)    throws  Exception { String[] parts = key.split( &quot;.&quot; ); if  (parts. length  == 1) {  //  erzeuge   Komponente Object component = Class. forName (value).newInstance(); components .put(parts[0], component); }  else  {  //  injiziere   Dependency Object component =  components .get(parts[0]); Object reference =  components .get(value); PropertyUtils. setProperty (component, parts[1], reference); } } }
Reportgenerator (8) Schritt 3: Realisiere DI via Reflection (cont‘d) ,[object Object],[object Object],#  Definiere   neue   Komponente  &quot; reportGenerator &quot; reportGenerator= de.dlr.spring101.report.step03.HtmlReportGenerator #  Definiere   neue   Komponente  &quot; reportService &quot; reportService= de.dlr.spring101.report.step03.ReportService #  Injiziere   die   Komponente  &quot; reportGenerator &quot;  #  in   das   Attribut  &quot; reportGenerator &quot; reportService.reportGenerator= reportGenerator
Reportgenerator (9) Schritt 4: Konfiguration mit Spring Beans (UML) ,[object Object]
Reportgenerator (10) Schritt 4: Konfiguration mit Spring Beans (cont‘d) import  org.springframework.context.ApplicationContext; import  org.springframework.context.support.ClassPathXmlApplicationContext; public   class  Main { public   static   void  main(String[] args) { ApplicationContext context =    new  ClassPathXmlApplicationContext( &quot;beans.xml&quot; ); ReportService reportService =    (ReportService) context.getBean( &quot;reportService&quot; ); reportService.generateAnnualReport(2008); } }
Reportgenerator (10) Schritt 4: Konfiguration mit Spring Beans (cont‘d) <? xml   version = &quot;1.0&quot;   encoding = &quot;UTF-8&quot; ?> < beans   xmlns = &quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation = &quot;http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd&quot; > < bean   id = &quot;defaultGenerator&quot;     class = &quot;de.dlr.spring101.report.step04.PdfReportGenerator&quot; /> < bean   id = &quot;reportService&quot;     class = &quot;de.dlr.spring101.report.step04.ReportService&quot; > < property   name = &quot;reportGenerator&quot; > < ref   local = &quot;defaultGenerator&quot; /> </ property > </ bean > </ beans >
Beans & ihre Konfiguration in Spring
Begrifflichkeiten Spring und J2EE POJO vs. JavaBean vs. (Spring) Bean ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Komponenten in Spring Spring Beans
Bean Definition (1) Bekanntmachen von Komponenten ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Bean Definition (2) Beispiel einer XML-Konfiguration
Bean-Definition (3) Zum Beispiel gehörige Klassendefinition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Konfiguration des Spring IoC-Containers BeanFactory vs. Application Context ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Initialisierung des Spring IoC-Containers  Lokalisieren, Erzeugen und Verwalten von Beans ,[object Object],[object Object],[object Object]
Erzeugung von Beans (1) Ablauf ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Erzeugung von Beans (2) Zeitpunkt der Initialisierung ,[object Object],[object Object],[object Object]
Injizierung von Beans Ablauf ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Setzen von Bean-Attributen Bean Properties ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JavaBean Antipattern Überprüfen von gesetzten Attributen via Spring ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lifecycle-Management von Beans via Interface ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lifecycle-Management von Beans via Konfiguration oder Annotation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Zusammenfassung & Ressourcen
Spring Core (DI) Alternativen ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Spring Framework Pros and Cons ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Literatur Webressourcen & Artikel ,[object Object],[object Object],[object Object],[object Object]
Literatur Bücher zum Thema ,[object Object],[object Object]
Vielen Dank.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (6)

Architektur einer Eclipse DLTK IDE für Clojure
Architektur einer Eclipse DLTK IDE für ClojureArchitektur einer Eclipse DLTK IDE für Clojure
Architektur einer Eclipse DLTK IDE für Clojure
 
javaPersistenceInActionFeaturesJenseitsDesEntryLevels
javaPersistenceInActionFeaturesJenseitsDesEntryLevelsjavaPersistenceInActionFeaturesJenseitsDesEntryLevels
javaPersistenceInActionFeaturesJenseitsDesEntryLevels
 
Bkr Workflow Oeffentlich
Bkr Workflow OeffentlichBkr Workflow Oeffentlich
Bkr Workflow Oeffentlich
 
Codesmells
CodesmellsCodesmells
Codesmells
 
Automatisierungmit NANT
Automatisierungmit NANTAutomatisierungmit NANT
Automatisierungmit NANT
 
Ruby on Rails SS09 05
Ruby on Rails SS09 05Ruby on Rails SS09 05
Ruby on Rails SS09 05
 

Andere mochten auch

Andere mochten auch (7)

주니어 개발자도 이해 할 수 있는 의존성 주입(Dependency Injection)
주니어 개발자도 이해 할 수 있는 의존성 주입(Dependency Injection)주니어 개발자도 이해 할 수 있는 의존성 주입(Dependency Injection)
주니어 개발자도 이해 할 수 있는 의존성 주입(Dependency Injection)
 
The Near Future of CSS
The Near Future of CSSThe Near Future of CSS
The Near Future of CSS
 
The Presentation Come-Back Kid
The Presentation Come-Back KidThe Presentation Come-Back Kid
The Presentation Come-Back Kid
 
Classroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and AdolescentsClassroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and Adolescents
 
Activism x Technology
Activism x TechnologyActivism x Technology
Activism x Technology
 
How to Battle Bad Reviews
How to Battle Bad ReviewsHow to Battle Bad Reviews
How to Battle Bad Reviews
 
The Buyer's Journey - by Chris Lema
The Buyer's Journey - by Chris LemaThe Buyer's Journey - by Chris Lema
The Buyer's Journey - by Chris Lema
 

Ähnlich wie 2009 03 17 Spring101

Tech Talk Spring Framework
Tech Talk Spring FrameworkTech Talk Spring Framework
Tech Talk Spring Framework
Jens Rühmkorf
 
Lightweight AOP with CDI and JPA
Lightweight AOP with CDI and JPALightweight AOP with CDI and JPA
Lightweight AOP with CDI and JPA
mh0708
 
dotnet Cologne 2013 - Windows Azure Mobile Services
dotnet Cologne 2013 - Windows Azure Mobile Servicesdotnet Cologne 2013 - Windows Azure Mobile Services
dotnet Cologne 2013 - Windows Azure Mobile Services
Sascha Dittmann
 

Ähnlich wie 2009 03 17 Spring101 (20)

Tech Talk Spring Framework
Tech Talk Spring FrameworkTech Talk Spring Framework
Tech Talk Spring Framework
 
Go - Googles Sprache für skalierbare Systeme
Go - Googles Sprache für skalierbare SystemeGo - Googles Sprache für skalierbare Systeme
Go - Googles Sprache für skalierbare Systeme
 
MVVM mit WPF
MVVM mit WPFMVVM mit WPF
MVVM mit WPF
 
Ionic 2 - Hybridapps auf Steroiden
Ionic 2 - Hybridapps auf SteroidenIonic 2 - Hybridapps auf Steroiden
Ionic 2 - Hybridapps auf Steroiden
 
Warum empfehle ich meinen Kunden das Spring Framework?
Warum empfehle ich meinen Kunden das Spring Framework? Warum empfehle ich meinen Kunden das Spring Framework?
Warum empfehle ich meinen Kunden das Spring Framework?
 
Einsteiger Workshop
Einsteiger WorkshopEinsteiger Workshop
Einsteiger Workshop
 
Große Applikationen mit AngularJS
Große Applikationen mit AngularJSGroße Applikationen mit AngularJS
Große Applikationen mit AngularJS
 
Automatischer Build mit Maven - OPITZ CONSULTING - Stefan Scheidt
Automatischer Build mit Maven - OPITZ CONSULTING - Stefan ScheidtAutomatischer Build mit Maven - OPITZ CONSULTING - Stefan Scheidt
Automatischer Build mit Maven - OPITZ CONSULTING - Stefan Scheidt
 
Lightweight AOP with CDI and JPA
Lightweight AOP with CDI and JPALightweight AOP with CDI and JPA
Lightweight AOP with CDI and JPA
 
GWT – Google Web Toolkit in der Praxis
GWT – Google Web Toolkit in der PraxisGWT – Google Web Toolkit in der Praxis
GWT – Google Web Toolkit in der Praxis
 
WPF Dos n Don'ts - der WPF Rundumschlag
WPF Dos n Don'ts - der WPF RundumschlagWPF Dos n Don'ts - der WPF Rundumschlag
WPF Dos n Don'ts - der WPF Rundumschlag
 
Workshop zu Hibernate 3.2.2 GA
Workshop zu Hibernate 3.2.2 GAWorkshop zu Hibernate 3.2.2 GA
Workshop zu Hibernate 3.2.2 GA
 
Feature Flags mit Togglz
Feature Flags mit TogglzFeature Flags mit Togglz
Feature Flags mit Togglz
 
Feige sein! Testen im Java-EE-Umfeld
Feige sein! Testen im Java-EE-UmfeldFeige sein! Testen im Java-EE-Umfeld
Feige sein! Testen im Java-EE-Umfeld
 
dotnet Cologne 2013 - Windows Azure Mobile Services
dotnet Cologne 2013 - Windows Azure Mobile Servicesdotnet Cologne 2013 - Windows Azure Mobile Services
dotnet Cologne 2013 - Windows Azure Mobile Services
 
MVVM Pattern
MVVM Pattern MVVM Pattern
MVVM Pattern
 
Automatischer Build mit Maven
Automatischer Build mit MavenAutomatischer Build mit Maven
Automatischer Build mit Maven
 
Sicherheit in Single-Page-Web-Anwendungen
Sicherheit in Single-Page-Web-AnwendungenSicherheit in Single-Page-Web-Anwendungen
Sicherheit in Single-Page-Web-Anwendungen
 
JavaScript: Von einfachen Scripten zu komplexen Anwendungen
JavaScript: Von einfachen Scripten zu komplexen AnwendungenJavaScript: Von einfachen Scripten zu komplexen Anwendungen
JavaScript: Von einfachen Scripten zu komplexen Anwendungen
 
Java Code Quality: Gute Software braucht guten Code - Regeln für verständlich...
Java Code Quality: Gute Software braucht guten Code - Regeln für verständlich...Java Code Quality: Gute Software braucht guten Code - Regeln für verständlich...
Java Code Quality: Gute Software braucht guten Code - Regeln für verständlich...
 

2009 03 17 Spring101

  • 1. Spring Framework Jens Rühmkorf Tech Talk, DLR Köln-Porz, 17. März 2009
  • 2.
  • 4.
  • 5.
  • 6. Übersicht des Spring Frameworks http://static.springframework.org/spring/docs/2.5.x/reference/
  • 7.
  • 8.
  • 9.
  • 10.
  • 12. „ Separation of Concerns “ (Dijkstra, 1974)
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 27.
  • 28.
  • 29.
  • 30. Reportgenerator (3) Schritt 1: Führe Container ein (cont‘d) public class Container { public static Container instance ; private Map<String, Object> components ; public Container() { components = new HashMap<String, Object>(); instance = this ; components .put( &quot;reportGenerator&quot; , new PdfReportGenerator()); components .put( &quot;reportService&quot; , new ReportService()); } public Object getComponent(String id) { return components .get(id); } }
  • 31.
  • 32. Reportgenerator (5) Schritt 2: Verwende Service-Locator Pattern (cont‘d) public class ServiceLocator { private static Container container = Container. instance ; public static ReportGenerator getReportGenerator() { return (ReportGenerator) container .getComponent( &quot;reportGenerator&quot; ); } } public class ReportService { private ReportGenerator reportGenerator = ServiceLocator. getReportGenerator (); // Rest weggelassen }
  • 33.
  • 34. Reportgenerator (7) Schritt 3: Realisiere DI via Reflection (cont‘d) public class Container { // Ausschnitt! private void processEntry(String key, String value) throws Exception { String[] parts = key.split( &quot;.&quot; ); if (parts. length == 1) { // erzeuge Komponente Object component = Class. forName (value).newInstance(); components .put(parts[0], component); } else { // injiziere Dependency Object component = components .get(parts[0]); Object reference = components .get(value); PropertyUtils. setProperty (component, parts[1], reference); } } }
  • 35.
  • 36.
  • 37. Reportgenerator (10) Schritt 4: Konfiguration mit Spring Beans (cont‘d) import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( &quot;beans.xml&quot; ); ReportService reportService = (ReportService) context.getBean( &quot;reportService&quot; ); reportService.generateAnnualReport(2008); } }
  • 38. Reportgenerator (10) Schritt 4: Konfiguration mit Spring Beans (cont‘d) <? xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot; ?> < beans xmlns = &quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation = &quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot; > < bean id = &quot;defaultGenerator&quot; class = &quot;de.dlr.spring101.report.step04.PdfReportGenerator&quot; /> < bean id = &quot;reportService&quot; class = &quot;de.dlr.spring101.report.step04.ReportService&quot; > < property name = &quot;reportGenerator&quot; > < ref local = &quot;defaultGenerator&quot; /> </ property > </ bean > </ beans >
  • 39. Beans & ihre Konfiguration in Spring
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 55.
  • 56.
  • 57.
  • 58.