220 likes | 414 Views
Spring Framework Fundamentals. March, 2006 Larry Hamel Codeguild, Inc. http://codeguild.com/. Framework Defined. A framework provides tools, designs, templates, and practices to somehow make development easier
E N D
Spring Framework Fundamentals March, 2006 Larry Hamel Codeguild, Inc. http://codeguild.com/
Framework Defined • A framework provides tools, designs, templates, and practices to somehow make development easier • Java frameworks tend to divide into front-end (display), middle (control logic), and/or back-end (persistance) functions
Struts (2001) Avalon (1999) Cocoon (1999) Expresso (2000) Webwork JavaServer Faces (2004)/Shale Seam (2005) Spring (2002) Tapestry Struts Action Framework (from Webwork) 2005 Hibernate Java Frameworks(parial list)
Outline • Two criteria for judging a framework • Loose coupling/Inversion of Control (IoC)/Dependency injection • Aspect-oriented programming (AOP) • Choosing a framework
Why is IoC important? • Integrate with other frameworks, other services • plug in a persistence layer, a display layer, etc. • Extensive unit-test coverage of framework code • the less dependence between classes, the easier to test
Interface vs. Concrete • Interface is a specification of signatures—no implementation • Concrete class has implementation of every method; can be instantiated • (Abstract class is in between—some, but not all, methods implemented)
Tight Coupling class Foo { void bar() { FastStringBuffer aBuffer = FastStringBuffer. getInstance(); // ... } }
Loose Coupling StringBufferFactoryInterface aBuffFac = getStringBuffer(); ------------------------------------ public StringBufferFactoryInterfacegetStringBuffer() { /* ??? how implement with no dependence ??? */ }
Loose Coupling (IoC) StringBufferFactoryInterface aBuffFac = getStringBuffer(); ----------------------------------------- public StringBufferFactoryInterfacegetStringBuffer() { return stringBufferFac; } public void setStringBuffer( StringBufferFactoryInterface aBuff) { stringBufferFac = aBuff; } private StringBufferFactoryInterfacestringBufferFac;
Spring configuration, injecting dependencies <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN“ ...> <beans> <bean id=“fastStringBuffer” class=“FastStringBuffer” /> <bean id=“myClient” class=“Foo”> <property name=“stringBuffer"> <ref local="fastStringBuffer"/> </property> </bean> </beans>
Spring is an IoC Bean Factory InputStream is = new FileInputStream("src/examples/spring/beans.xml"); BeanFactory factory = new XmlBeanFactory(is); DataProcessor dataProcessor = (DataProcessor) factory.getBean("fileDataProcessor"); Result result = dataProcessor.processData();
Aspect-Oriented Programming (AOP) Rationale • Object oriented programming (OOP) isn’t good at everything. • Consider: • Security: want to test privileges every time method is invoked. • DB transactions: want to have every access to DB run through transaction
Transaction OO example DBConnection conn = DBConnectionPool.getInstance() .getConnection("course clone"); try { conn.setAutoCommit(false); Course clonecourse = new Course(conn); ... } catch (Exception e) { conn.rollback(); getLogger().error(e); } finally { conn.release(); }
AOP • Transactions, Logging and Security are often referred to as “crosscutting” concerns, applying to all objects • For every method, AOP inserts ‘advice’ before and/or after method call • Spring uses dynamic AOP, creating “proxies” for all advised objects
AOP Example in Spring • Using JDK1.5 annotations! @Transactional public void saveCourse( Course src) { getCourseDBO().save(src); }
Choosing the Right Tool • If you already know tool ++ • Currently popular tool ++ • Estimate lifespan of project • Estimate audience (users) • Estimate need to interface with external resources (email, LDAP) • Impartial, comprehensive reviews are hard to find • Any framework will go obsolete!
Opinion • Is two-tier PHP good enough? • Bigger projects: take care not to overestimate—”You’re not going to need it” • Watch: • Ruby on Rails (David Hansson) • Seam (Gavin King) • Spring (Rod Johnson)