Title: Putting some Spring in your step
1Putting some Spring in your step
- Incremental adoption of Spring
2Revolutions and Evolutions
- Spring as a Revolutionary Technology?
- Maybe.
- For today, lets consider it merely as a
collection of convenient implementations enabling
us to evolve our programming.
3Aside High code quality
- Testcases, community, standards, review.
- Iron fisted control of a few folks with way too
much spare time. - Whatever you credit, objectively this code is
solid.
4But what does it do?
AOP Source-level metadata AOP Infrastructure
JDBC Core, Object, Transaction
O R M
Web MVC MVC Views Supports JSP / JSTL PDF /
Excel Velocity / Struts Whatever
Web utilities
Context ApplicationContext JNDI support UI
support Mail
DAO DataAccessException
CORE Supporting Utilities, Bean Container
5AOP
- Aspect oriented programming
- Say what???
- Dynamic proxies. Interceptors. Pointcuts.
6Core
7DAO
- Support for your Data Access Objects.
8JDBC
- Takes the boilerplate out of boilerplate JDBC
code.
9ORM
- Object relational mapping support and abstraction
10Context ApplicationContext
- Beyond the beans
- MessageSource
- Email sending
- Localization / Internationalization
- User interface Themes, etc.
11Web utilities
12Spring Web MVC
- Not just any old Web MVC implementation
13That was Spring in a whirlwind
- Theres a lot here
- But lets focus on just enough to get you to
install it. - You can then be pleasantly surprised to find out
that it does all the rest too.
14Not all the rest
- Spring only sets out to do what they think others
have not done / not done well. - Doesnt reimplement logging, connection pooling,
testing, Commons projects, etc.
15Theme Incremental Improvement
- Walk before you can run.
- Crawl before you walk.
- Drool before you crawl?
- Wherever you think you are, Spring has something
to offer.
16Previous Presentation
- Spring Theory
- Hollywood Principle
17Dont call us, well call you.
Hollywood Principle
18Practice
- What is the Hollywood Principle?
19Dont call us, well call you.
Hollywood Principle
20Todays content
- Spring you can reach for and use
- Dependency Injection in action
- Spring you can use without reaching
- Spring JDBC
21Dependency Injection
- Hollywood approach to dependencies well call
you to inject your dependencies.
22Feels great!
- Express dependencies as Interfaces
- Write classes with fewer concrete dependencies
- Much more testable code.
23BeanFactory
- Report from the frontlines this works.
- Works better than you might expect.
24applicationContext.xml
- ltbean idfoo classedu.yale.Foo /gt
- ltbean idbar classedu.yale.Bargt
- ltproperty namefoogt
- ltref idfoo/gt
- lt/propertygt
- lt/beangt
25Aside Powerful Bean declaration
- Singletons and Prototypes
- Static factories (say Ugh!)
- Instance factories
- Constructor arguments
- JavaBean Properties
- Lists, Maps, Properties, MessageBundles
- Lookups (e.g. JNDI)
26Where does Spring put it?
Spring ContextLoaderListener
Servlet Context
Spring ApplicationContext
ltbean idfoo /gt ltbean idbar .../gt ltbean
idwombat /gt
27How do I get it?
Servlet Context
ApplicationContext
WebApplicationContextUtils
Your Servlet
28Poor Mans Dependency Injection
- Javas a pretty nice language
- You can write your own ContextListener to
instantiate your interfaces. - You can have separation of concerns, Dependency
Injection, interface-driven development w/o Spring
29Web MVC
- We have a lot of invested expertise in
- JSP / JSTL
- Bare Java Servlets
- Struts
- JSF?
- Whatever other web framework youve learned
30The theme was Evolution
- Keep using existing Web-tier technologies where
it makes sense to do so. - Keep writing / maintaining those Servlets /
Actions / whatevers - Potentially, adopt ApplicationContexts as the way
your Servlets/Actions/whatevers get access to
instances of Business Interfaces
31But even if you dont
- Spring we can use right now
32Data Access Objects
- Interfaces for your data access needs
- Read information from DB, write back to DB,
33DAOs plausibly implemented using JDBC
- Or Hibernate, or no DataSource at all, or
whatever
34But plausibly, JDBC
- Expertise
- Power
- Existence of / reusability of queries / stored
procedures - Institutional investment in Enterprise Database
35JDBC
- Writing raw JDBC is a pain
- Its easy to forget to close resources
- Boilerplate iteration over result sets
- Boilerplate checking result set size
- Boilerplate translation to/from Java objects
- SQLException
36Easy?
- Bold statement Spring provides exactly the JDBC
abstraction we would have written, all things
being ideal
37Okay wise guy
- What is it that we would have implemented?
38Dont call us, well call you.
Hollywood Principle
39JDBC in Hollywood
- Threadsafe framework classes that provide
boilerplate functionality and configuration or
callback. - Write once the thing that is always the same.
- Write each time exactly what you have to.
40A SQL Query
- SELECT foo, bar FROM splat WHERE wombat ?
41MappingSqlQuery
- public YourDaoImpl(DataSource ds)
- super(ds, SQL)
- super.declareParameter(
- new SQLParameter(name, TYPE)
- compile()
42Implement the abstract method
- protected Object mapRow(ResultSet rs, int rowNum)
throws SQLException - Splat splat new Splat()
- splat.setFoo(rs.getBoolean(foo))
- splat.setBar(rs.getSomeType(bar))
- return splat
43What you just got
- ResultSet, PreparedStatement, Connection closed
properly. - Detailed, vendor-specific, undeclared aka
Runtime aka Unchecked exceptions extending
DataAccessException. - Logging. Of the actual SQL, if you crank up the
logging level.
44What you just got
- Threadsafe, reusable Java object representing
your query
45Same pattern for
- Stored Procedures
- Updates
- Functions
- Queries that dont map to Java objects
46Incidentally
- Spring would love to make you a DataSource
outside of your ServletContainer - Spring would love to be the source for that
DataSource you supplied your DAO implementation.
47Template Method Design Pattern
- Spring is using this design pattern here
- The pattern is applicable elsewhere
- E.g. CAS PasswordHandler
48Dont call us, well call you.
Hollywood Principle
49Stored Procedure Example
- Netregs getPersonRole takes a netid, returns a
role.
50DAO interface
- Public interface NetregRoleDao
- public String roleForPerson(String netid)
51Constructor
- SQLNETREG.API.getPersonRole
- public RoleStoredProc(DataSource ds)
- super(ds, SQL)
- declareParameter(new SqlOutParameter(role,
Types.VARCHAR) - compile()
52DAO interface implementation
- public String roleForPerson(String netid)
- Map params new HashMap()
- params.put(netid, netid)
- Map results execute(params)
- return (String) results.get(role)
53You can use this today
- The creative work is writing the query and the
translation from the Row to the fulfillment of
the DAO interface. - The boilerplate is done for you.
54And now that youve installed it
- Check out what else Spring offers.