spring and tdd
TRANSCRIPT
-
8/7/2019 Spring and TDD
1/20
Facilitate TDD with the
Spring FrameworkBy: Mike Shoemaker
-
8/7/2019 Spring and TDD
2/20
What is Spring?
Spring provides a light-weight solution for building
enterprise-ready applications, while still supporting thepossibility of using declarative transaction management,remote access to your logic using RMI or webservices,mailing facilities and various options in persisting yourdata to a database. Spring provides an MVC framework,
transparent ways of integrating AOP into your software anda well-structured exception hierarchy including automaticmapping from proprietary exception hierarchies.
-
8/7/2019 Spring and TDD
3/20
Spring Modules
-
8/7/2019 Spring and TDD
4/20
Springs Mission? J2EE should be easier to use
It's best to program to interfaces, rather thanclasses. Spring reduces the complexity cost of using interfaces to zero.
JavaBeans offer a great way of configuringapplications.
OO design is more important than anyimplementation technology, such as J2EE.
Checked exceptions are overused in Java. A
framework shouldn't force you to catch exceptionsyou're unlikely to be able to recover from.
Testability is essential, and a framework such asSpring should help make your code easier to test.
-
8/7/2019 Spring and TDD
5/20
Lightweight Container?
Core usage does not impose unwantedcompile time dependencies like EJB
Loosely Coupled Components
Unit testing is possible againPOJOs
-
8/7/2019 Spring and TDD
6/20
Role of the Container
AKA BeanFactory
Loads Bean denitions ( XML )Instantiates Beans on behalf of client
Congure dependencies via XML
Replaces Factory methods for object creation
-
8/7/2019 Spring and TDD
7/20
Singleton Beans
Default conguration
Bean instance is shared
Lifecycle is managed by BeanFactory
EfcientGood for Stateless operations
-
8/7/2019 Spring and TDD
8/20
Prototype Beans
BeanFactory offers a new instance upon eachclient request
BeanFactory cannot manage lifecycle since itloses track of the bean after it hands it off
Semantically equal to the new keyword
-
8/7/2019 Spring and TDD
9/20
Sample Bean
package com.acme;
public class MyBean {private String state;
public String getState() {return state;
}
public void setState(String state) {this.state = state;
} }
-
8/7/2019 Spring and TDD
10/20
How do I get a bean?// String Array of bean denition leString[] paths = {"applicationContext.xml"};
// Load bean denitionsApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
// Code to retrieve bean from BeanFactoryMyBean bean = (MyBean)ctx.getBean(myBean);
-
8/7/2019 Spring and TDD
11/20
ApplicationContext
Extends BeanFactory functionality
Can initialize singleton beans upon load
Can lazy load beans
Provides i18n supportSupports lifecycle events
-
8/7/2019 Spring and TDD
12/20
-
8/7/2019 Spring and TDD
13/20
Dependency Injection
Synonymous with Inversion of Control (IOC)
Sometimes referred to as JNDI in reverse
Hollywood Principal
Dont call me, Ill call youDI coined by Martin Fowler
-
8/7/2019 Spring and TDD
14/20
Types of Dependency
InjectionInterface Dependent ( BAD )
AvalonConstructor Based
Pico Container
Setter Based
Spring
-
8/7/2019 Spring and TDD
15/20
Example of Constructor
Based Injectioncom.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/myDSfredpassword
-
8/7/2019 Spring and TDD
16/20
Datasource datasource = .........
// Instantiate passing datasource in constructorLoginDAO dao = new LoginDAOJDBC(datasource);
Equivalent Java Code
-
8/7/2019 Spring and TDD
17/20
Example of Setter
Based Injection
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/myDSfredpassword
-
8/7/2019 Spring and TDD
18/20
Equivalent Java CodeDatasource datasource = .........
// InstantiateLoginDAO dao = new LoginDAOJDBC();
// set data source that was acquired previouslydao.setDatasource(datasource);
-
8/7/2019 Spring and TDD
19/20
References
http://www.springframework.orgSpring Live
Spring In Action
Pro Spring
-
8/7/2019 Spring and TDD
20/20
Group Exercise
Create Car Insurance Application withoutSpring
Once its working, introduce Spring
Discuss Other Possibilities