0% found this document useful (0 votes)
949 views

The Spring Framework: Simplifying J2EE

Spring is a J2EE framework designed to make building applications easier. It's best to program to interfaces, rather than classes. Spring reduces the complexity of using interfaces to zero. A la carte framework that allows you to pick and choose features to use.

Uploaded by

bmkhatri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
949 views

The Spring Framework: Simplifying J2EE

Spring is a J2EE framework designed to make building applications easier. It's best to program to interfaces, rather than classes. Spring reduces the complexity of using interfaces to zero. A la carte framework that allows you to pick and choose features to use.

Uploaded by

bmkhatri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

The Spring Framework

Simplifying J2EE

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Introductions
Your experience with Spring?
Your experience with J2EE?
What do you hope to learn today?
Open Source experience: Ant, XDoclet, Hibernate?
Favorite IDE? Favorite OS?

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Who is Matt Raible?
Developing websites since 1994 - Developing J2EE
webapps since 1999
Committer on several open source projects:
AppFuse, Roller Weblogger, XDoclet, Struts Menu,
Display Tag
J2EE 5.0 and JSF 1.2 Expert Group Member
Author: Spring Live (SourceBeat) and contributor to
Pro JSP (Apress)

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Spring Mission Statement
J2EE should be easier to use.
It’s best to program to interfaces, rather than
classes. Spring reduces the complexity of using
interfaces to zero.
JavaBeans offer a great way of configuring
applications.
OO Design is more important than any
implementation technology, such as J2EE.

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Spring Mission Statement
Checked exceptions are overused in Java. A
framework shouldn’t force you to catch exception
you’re unlikely to recover from.

Testability is essential, and a framework such as


Spring should help make your code easier to test.

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


What is Spring?
A J2EE Framework designed to make building
applications easier
Provides a means to manage your business objects
and their dependencies
Inversion of Control allows classes to be loosely
coupled and dependencies written in XML
À la carte framework that allows you to pick and
choose features to use

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Spring Modules

www.sourcebeat.com © 2005, Virtuas, LLC www.virtuas.com


Sample Architecture

www.sourcebeat.com © 2005, Virtuas, LLC www.virtuas.com


The BeanFactory

www.sourcebeat.com © 2005, Virtuas, LLC www.virtuas.com


Dependency Injection
Hollywood Principle:
“Don’t call me, I’ll call you.”
http://martinfowler.com/articles/injection.html
Lookup dependencies vs. Exposing dependencies
Easy to mock dependencies (i.e. in unit tests)
Spring supports constructor injection, setter
injection and method injection

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Example: Controller and DAO
Controller has a setter or a constructor argument for
the DAO
DAO is configured as a dependency
<bean id="userController" class="org.appfuse.web.UserController">
<property name="userDAO" ref="userDAO"/>
</bean>

<bean id="userDAO" class="org.appfuse.dao.UserDAOHibernate">


<property name="sessionFactory" ref="sessionFactory"/>
</bean>

Makes it easy to swap out implementations


Tests can verify operations succeed on the interface

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Code before IoC
public class UserController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request,


HttpServletResponse response)
throws Exception {
Connection conn = DatabaseUtils.getConnection();
UserDAO dao = DAOFactory.createUserDAO("hibernate", conn);

List users = dao.getUsers();

DatabaseUtils.closeConnection(conn);

return new ModelAndView("userList", "users", users);


}
}

www.sourcebeat.com © 2005, Virtuas, LLC www.virtuas.com


Code after IoC
public class UserController implements Controller {
private UserDAO dao = null;

public void setUserDAO(UserDAO userDAO) {


this.dao = userDAO;
}

public ModelAndView handleRequest(HttpServletRequest request,


HttpServletResponse response)
throws Exception {

List users = dao.getUsers();

return new ModelAndView("userList", "users", users);


}
}

www.sourcebeat.com © 2005, Virtuas, LLC www.virtuas.com


Spring vs. J2EE
It’s not really a comparison, Spring builds off J2EE
and merely makes the APIs easier
It’s still using many of J2EE’s and Java’s underlying
infrastructure
EJB vs. Spring Managed POJOs
Opinions and Hesitations?

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Spring simplifies EJBs
Abstract classes in org.springframework.ejb.support
override methods that you normally don’t
implement
Expose EJBs as Spring beans with classes in
org.springframework.ejb.access package
<bean id="simpleService" lazy-init="true"
class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="jndiName" value="jndiName"/>
<property name="cacheHome" value="true"/>
<property name="businessInterface" value="org.appfuse.ejb.SimpleService"/>
</bean>

Configuring a JtaTransactionManager will allow


Spring beans to participate in CMT

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Spring MVC
Front-Controller Servlet: DispatcherServlet
Controllers loaded as beans from servlet’s XML
context files
ContextLoaderListener loads other XML context
files
Many different Controller options: Controller,
SimpleFormController, Wizard

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Controllers
The Controller interface is generally used to display
data, not for forms
SimpleFormController easiest for forms
Views: JSP Tags, Tiles, Velocity, FreeMarker, PDF,
Excel
Validation: Commons Validator or Validation
classes

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


PropertyEditors
PropertyEditors allow you to convert Dates, Integers
and custom types
Built-in PropertyEditor examples:
ByteArrayPropertyEditor (file upload),
CustomDateEditor, CustomNumberEditor
Register for context files by defining
CustomEditorConfigurer bean
Register in Spring MVC in initBinder() method of
SimpleFormController

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Page Decoration and
Composition
Tiles vs. SiteMesh
Tiles works great for page composition - soon to be
come top-level project at Apache
SiteMesh is easiest to use for page decoration
Can be used together or separately

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Spring Supports many
Java MVC Frameworks
ContextLoaderListener loads beans into
ServletContext - can retrieve with:
WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);

Struts has ContextLoaderPlugin and ActionSupport


classes
WebWork has SpringObjectFactory
Tapestry - override BaseEngine and stuff the context
into the Global
JSF - DelegatingVariableResolver

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Integrating Struts
with Spring
ContextLoaderPlugin - match action “path” with bean
“name”
Override default RequestProcessor with Spring's
DelegatingRequestProcessor
Use the DelegatingActionProxy class in the "type"
attribute of your <action-mapping>
TIP: Load all context files in plugin to make testing with
StrutsTestCase easier
Use a Spring ActionSupport class (subclass of the standard
Struts Actions) and call getWebApplicationContext()

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


MVC Tips
Using IoC for setting Controller/Action
dependencies makes it easy to mock them
JSP 2.0 Tag files in JIRA - for syntax simplification
Use SiteMesh - simple yet powerful
AppFuse and Equinox have good integration
examples

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Data Access
Spring JDBC Framework
No exceptions to catch or resources to close
Base DAO and Template classes for many
frameworks:
Hibernate, iBATIS, OJB, JDO - even TopLink

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


DataAccessException

www.sourcebeat.com © 2005, Virtuas, LLC www.virtuas.com


Data Access Examples
UserDAOTest
Hibernate
iBATIS
Spring JDBC
JDO

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Transactions
Much easier to use than UserTransaction in J2EE
CMT-like capabilities with XML and transparent
AOP
Supports Commons Attributes and JDK 5
Annotations
Pluggable Transaction Managers, including JTA

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Declarative Transactions
<bean id="userManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref bean="userManagerTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>

<bean id="userManagerTarget" class="org.appfuse.service.impl.UserManagerImpl">


<property name="userDAO"><ref bean="userDAO"/></property>
</bean>

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Testing Spring Applications
Easy to mock dependencies with EasyMock and
jMock
Load context in tests and bind dependencies as you
would in production
TIP: Use a static block or Spring classes in Spring
Mock (in org.springframework.test package)

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


AOP
AOP - Reduces duplication among classes
Interceptors make it easy to add before, around and
after method advice
Useful for caching, logging and security
EHCache, Performance/Tracing interceptors,
Acegi for Security

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


App Server Support
Spring should run on any app server
Hooks into many app server’s Transaction Manager
Built-in Support in Geronimo
Survey: which app servers are you running it on?

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Tools
Spring IDE Plugin For Eclipse
http://springide.org
BeanDoc
http://opensource.atlassian.com/confluence/spring/
display/BDOC/Home
Gaijin Studio
http://gaijin-studio.sf.net
Screenshots ➪

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com


Resources
Web: www.springframework.org and
forum.springframework.org
Print: Spring Live, Pro Spring, Spring in Action and
Professional Spring Development
Examples: AppFuse (appfuse.dev.java.net), Equinox
(equinox.dev.java.net), JPetstore
Training: Interface21, Virtuas, Bouvet, Arc-Mind

www.springlive.com © 2005, Virtuas, LLC www.virtuas.com

You might also like