Copyright 2006, Codeguild, Inc Spring Framework Fundamentals March, 2006 Larry Hamel Codeguild, Inc.
Copyright 2006, Codeguild, Inc 2 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
Copyright 2006, Codeguild, Inc 3 Java Frameworks (parial list) 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
Copyright 2006, Codeguild, Inc 4 Outline Two criteria for judging a framework –Loose coupling/Inversion of Control (IoC)/Dependency injection –Aspect-oriented programming (AOP) Choosing a framework
Copyright 2006, Codeguild, Inc 5 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
Copyright 2006, Codeguild, Inc 6 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)
Copyright 2006, Codeguild, Inc 7 Tight Coupling class Foo { void bar() { FastStringBuffer aBuffer = FastStringBuffer. getInstance(); //... }
Copyright 2006, Codeguild, Inc 8 Loose Coupling StringBufferFactoryInterface aBuffFac = getStringBuffer(); public StringBufferFactoryInterface getStringBuffer() { /* ??? how implement with no dependence ??? */ }
Copyright 2006, Codeguild, Inc 9 Loose Coupling (IoC) StringBufferFactoryInterface aBuffFac = getStringBuffer(); public StringBufferFactoryInterface getStringBuffer() { return stringBufferFac; } public void setStringBuffer( StringBufferFactoryInterface aBuff) { stringBufferFac = aBuff; } private StringBufferFactoryInterface stringBufferFac;
Copyright 2006, Codeguild, Inc 10 Spring configuration, injecting dependencies <bean id=“fastStringBuffer” class=“FastStringBuffer” />
Copyright 2006, Codeguild, Inc 11 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("fileDataProce ssor"); Result result = dataProcessor.processData();
Copyright 2006, Codeguild, Inc 12 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
Copyright 2006, Codeguild, Inc 13 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(); }
Copyright 2006, Codeguild, Inc 14 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
Copyright 2006, Codeguild, Inc 15 AOP Example in Spring Using JDK1.5 public void saveCourse( Course src) { getCourseDBO().save(src); }
Copyright 2006, Codeguild, Inc 16 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 ( , LDAP) Impartial, comprehensive reviews are hard to find Any framework will go obsolete!
Copyright 2006, Codeguild, Inc 17 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)
Copyright 2006, Codeguild, Inc 18