my daily spring - best practices with the springframework
TRANSCRIPT
Thorsten Kamann ● [email protected] 11
Best Practices mit dem Spring Framework
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
Thorsten Kamann ● [email protected] 33
Die Spring-IDE
Thorsten Kamann ● [email protected] 44
Spring-IDE: Features
Spring 2.5 Spring AOPSpring
Webflow
Spring Explorer
Bean Editor Suchen
Visualisierung
Thorsten Kamann ● [email protected] 55
Spring-IDE: Spring-Explorer
Beans
Properties
Doppelklicköffnet
Element
Thorsten Kamann ● [email protected] 66
Spring-IDE: Project-Explorer
Beans
Properties
Doppelklicköffnet
Element
Thorsten Kamann ● [email protected] 77
Spring-IDE: Beans-Editor
Erweiterter WTP-XML
Editor
XML-Schema
Validierung
Thorsten Kamann ● [email protected] 1212
Spring-IDE: Beans-Editor
Marker für Aspekte
Marker für Aspekte
Thorsten Kamann ● [email protected] 1414
Spring-IDE: Suche
Thorsten Kamann ● [email protected] 1515
Spring-IDE: Visualisierung
Erzeugt grafische Repräsentation
Thorsten Kamann ● [email protected] 1616
DEMO
Thorsten Kamann ● [email protected] 1818
Architekturen: Mit oder ohne Spring?
Templates
*AwareIF
+Ohne Spring nicht compilefähig
Tests nur mit Spring
-
Thorsten Kamann ● [email protected] 1919
Architekturen: Templates
Vereinfachung
Kapselung
Vereinheitlichung
Thorsten Kamann ● [email protected] 2020
Architekturen: Alternative JPA
Standardisiert
EntityManager
Keine Abhängigkeiten
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();
}
}
Thorsten Kamann ● [email protected] 2222
Architekturen: Standards
Austauschbare Implementierungen
Zukunftssicher
Know-How
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
-
Thorsten Kamann ● [email protected] 2424
Architekturen: Spring-Annotations
@Target( { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
String value() default "";
}
Thorsten Kamann ● [email protected] 2525
Architekturen: Spring-Annotations
<bean
class=
„…PersistenceExceptionTranslationPostProcessor„
p:repositoryAnnotationType=
„…Repository"/>
Thorsten Kamann ● [email protected] 2626
Architekturen: Spring-Annotations
import ...annotations.Repository;
@Repository
public class CustomerDaoImpl
implements CustomerDao {
…
}
Thorsten Kamann ● [email protected] 2727
Testen mit Spring
Thorsten Kamann ● [email protected] 2828
Testen mit Spring: Unittests
Effektive und aussagekräftige Unittest
PoJo
jUnitSpring
Thorsten Kamann ● [email protected] 2929
Testen mit Spring: Unittests
Angepasste Konfigurationen
Dependency Injection
Transaction Management
Annotations
Support Classes
Thorsten Kamann ● [email protected] 3030
Testen mit Spring: Unittests
@ContextConfiguration
• Ermöglicht testspezifische Konfigurationen
• Autom. Erkennung von Konfigurationen
• Angabe von mehreren Konfigurationen•@ContextConfiguration(locations={„…“})
Thorsten Kamann ● [email protected] 3131
Testen mit Spring: Unittests
@RunWith(SpringJUnit4ClassRunner)
@ContextConfiguration
public class CustomerDaoTest{
}
Spring Testrunner
Sucht CustomerDaoText-context.xml im gleichen Package
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
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
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
Thorsten Kamann ● [email protected] 3535
Testen mit Spring: Unittests
Transaction Management
•@Transactional
•@TransactionConfiguration
•@BeforeTransaction
•@AfterTransaction
•@Rollback
•@NotTransactional
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
Thorsten Kamann ● [email protected] 3737
Testen mit Spring: Unittests
Annotations
•@IfProfileValue
•@ProfileValueSourceConfiguration
•@ExpectedException
•@Timed
•@Repeat
Thorsten Kamann ● [email protected] 3838
Testen mit Spring: Unittests
Support Classes
• AbstractJUnit4SpringContextTests
• Zugriff auf• applicationContext.xml
• AbstractTransactionalJUnit4SpringContextTests
• Zugriff auf• applicationContext.xml
• simpleJdbcTemplate
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
Thorsten Kamann ● [email protected] 4040