my daily spring - best practices with the springframework

40
Thorsten Kamann [email protected] 1 1 Best Practices mit dem Spring Framework

Upload: thorsten-kamann

Post on 20-Aug-2015

1.569 views

Category:

Business


0 download

TRANSCRIPT

Page 1: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 11

Best Practices mit dem Spring Framework

Page 2: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 22

Inhalt

Testen mit Spring

Spring Test-XML DI Transaktionen Annotations

Architekturen unabhängig von Spring

Keine Templates Standards Annotations

Die Spring-IDE

Features Navigation Aspekte Visualisierung

Page 3: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 33

Die Spring-IDE

Page 4: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 44

Spring-IDE: Features

Spring 2.5 Spring AOPSpring

Webflow

Spring Explorer

Bean Editor Suchen

Visualisierung

Page 5: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 55

Spring-IDE: Spring-Explorer

Beans

Properties

Doppelklicköffnet

Element

Page 6: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 66

Spring-IDE: Project-Explorer

Beans

Properties

Doppelklicköffnet

Element

Page 7: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 77

Spring-IDE: Beans-Editor

Erweiterter WTP-XML

Editor

XML-Schema

Validierung

Page 8: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 88

Spring-IDE: Beans-Editor

STRG+SPACE

Page 9: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 99

Spring-IDE: Beans-Editor

STRG+SPACE

DokumentationP-Namespace

Page 10: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1010

Spring-IDE: Beans-Editor

F3

Page 11: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1111

Spring-IDE: Beans-Editor

AOP-SupportAOP-Support

Page 12: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1212

Spring-IDE: Beans-Editor

Marker für Aspekte

Marker für Aspekte

Page 13: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1313

Spring-IDE: Beans-Editor

Refactoring

Page 14: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1414

Spring-IDE: Suche

Page 15: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1515

Spring-IDE: Visualisierung

Erzeugt grafische Repräsentation

Page 16: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1616

DEMO

Page 17: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1717

Architekturen unabhängig von

Spring

Page 18: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1818

Architekturen: Mit oder ohne Spring?

Templates

*AwareIF

+Ohne Spring nicht compilefähig

Tests nur mit Spring

-

Page 19: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 1919

Architekturen: Templates

Vereinfachung

Kapselung

Vereinheitlichung

Page 20: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2020

Architekturen: Alternative JPA

Standardisiert

EntityManager

Keine Abhängigkeiten

Page 21: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2121

Architekturen: Alternative JPA (DAO)

@Repository

public class CustomerDaoImpl implements

CustomerDao {

@PersistenceContext(unitName = "mds-core")

protected EntityManager entityManager;

public List<Customer> findAllCustomers() {

return entityManager.createQuery(

"from CUSTOMER c").getResultList();

}

}

Page 22: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2222

Architekturen: Standards

Austauschbare Implementierungen

Zukunftssicher

Know-How

Page 23: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2323

Architekturen: Spring-Annotations

Direkt einsetzbar

Keine zus. Konfiguration,Aufwände

+Abhängig von Spring

Komponente in anderen Kontext nicht möglich

-

Page 24: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2424

Architekturen: Spring-Annotations

@Target( { ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Component

public @interface Repository {

String value() default "";

}

Page 25: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2525

Architekturen: Spring-Annotations

<bean

class=

„…PersistenceExceptionTranslationPostProcessor„

p:repositoryAnnotationType=

„…Repository"/>

Page 26: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2626

Architekturen: Spring-Annotations

import ...annotations.Repository;

@Repository

public class CustomerDaoImpl

implements CustomerDao {

}

Page 27: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2727

Testen mit Spring

Page 28: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2828

Testen mit Spring: Unittests

Effektive und aussagekräftige Unittest

PoJo

jUnitSpring

Page 29: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 2929

Testen mit Spring: Unittests

Angepasste Konfigurationen

Dependency Injection

Transaction Management

Annotations

Support Classes

Page 30: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3030

Testen mit Spring: Unittests

@ContextConfiguration

• Ermöglicht testspezifische Konfigurationen

• Autom. Erkennung von Konfigurationen

• Angabe von mehreren Konfigurationen•@ContextConfiguration(locations={„…“})

Page 31: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3131

Testen mit Spring: Unittests

@RunWith(SpringJUnit4ClassRunner)

@ContextConfiguration

public class CustomerDaoTest{

}

Spring Testrunner

Sucht CustomerDaoText-context.xml im gleichen Package

Page 32: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3232

Testen mit Spring: Unittests

@RunWith(SpringJUnit4ClassRunner)

@ContextConfiguration(locations={„my1.xml“, „my2.xml“})

public class CustomerDaoTest{

}

Lädt die Konfiguration ausmy1.xml und my2.xml im gleichen

Package

Page 33: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3333

Testen mit Spring: Unittests

Dependency Injection

• Alle von Spring unterstützten Annotations können verwendet werden:•@Autowired, @Resource

• Keine Inheritance von Spring Basisklassen

• Reduziert den Kodierungsaufwand

• Keine Definition von Setter-Methoden erforderlich

Page 34: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3434

Testen mit Spring: Unittests

public class CustomerDaoTest{

@Autowired

CustomerDao customerDao

}

<bean id="CustomerDao"

class=„...mds.dataaccess.internal.CustomerDaoImpl"/>

Sucht Bean instanceof CustomerDao

Page 35: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3535

Testen mit Spring: Unittests

Transaction Management

•@Transactional

•@TransactionConfiguration

•@BeforeTransaction

•@AfterTransaction

•@Rollback

•@NotTransactional

Page 36: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3636

Testen mit Spring: Unittests

@TransactionConfiguration(

transactionManager="transactionManager")

@Transactional

public class CustomerDaoTest{

}

Markiert alle Methoden als Transactional

Verwendet den Transaction-Manager mit dem angegebenen

Namen

Page 37: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3737

Testen mit Spring: Unittests

Annotations

•@IfProfileValue

•@ProfileValueSourceConfiguration

•@ExpectedException

•@Timed

•@Repeat

Page 38: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3838

Testen mit Spring: Unittests

Support Classes

• AbstractJUnit4SpringContextTests

• Zugriff auf• applicationContext.xml

• AbstractTransactionalJUnit4SpringContextTests

• Zugriff auf• applicationContext.xml

• simpleJdbcTemplate

Page 39: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 3939

• Springframework Webseitehttp://www.springframework.org

• Spring IDE (Eclipse Plugin)http://www.springide.org

• Thorsten Kamannhttp://www.thorsten-kamann.de

Links

Page 40: My Daily Spring - Best Practices with the Springframework

Thorsten Kamann ● [email protected] 4040