Title: The Spring Framework J2EE without EJB
1The Spring FrameworkJ2EE without EJB
- Jürgen Höller
- http//www.springframework.com
- juergen_at_interface21.com
2Agenda
- J2EE Reviewed
- Introducing the Spring Framework
- Spring and J2EE
- Core Container
- AOP Framework
- Transactions Data Access
- Remoting
- Further Services
3J2EE Reviewed (1)
- Challenges of modern server-side software
development - multi-threaded execution
- resource and transaction management
- remote service access
- HTTP server integration
- Java 2 Enterprise Edition (J2EE tm)
- set of specifications for standard
applicationserver infrastructure - industry standard backed by Sun, IBM, Oracle
- application server implements standard APIs
- application uses standard APIs
4J2EE Reviewed (2)
- J2EE specifies system services
- Servlets, JSP, JTA, JDBC, JMS, JavaMail
- builds on underlying standard Java runtime
- J2EE also specifies component models
- Servlets for HTTP endpoints
- EJBs for middle tier components
- Focus on traditional 3-tier server layout
- focus on physical separation between tiers
- middle tier always requires application server
- Low-level APIs
- direct usage in applications often cumbersome
- comfortable high-level APIs?
5J2EE Reviewed (3)
- Scope of J2EE is limited
- EJBs are coarse-grained components
- mainly for (remote) service façades
- special deployment effort for every component
- How to deal with fine-grained,co-located
application objects? - forced to use custom solutions
- common open source libraries / frameworks
- How to run outside of an application server?
- not covered by traditional J2EE
- important for unit tests and integration tests
- also important for productive development
6Spring Framework (1)
- Java / J2EE Application Framework
- based on Rod Johnsons bookJ2EE Design
Development (Wiley, 2002) - current book "J2EE Development without EJB"(Rod
Johnson, Jürgen Höller Wiley, 2004) - Focus on "Plain Old Java Objects" (POJOs)
- natural, generic component model for applications
- flexible alternative to EJB, not tied to J2EE
- Open Source Project on SourceForge
- Apache license
- since February 2003
- 25 developers
7Spring Framework (2)
- Business objects as decoupled POJOs
- configuration and wiring through framework
- or usage as normal Java objects
- independent from the actual environment
- no unnecessary ties to a framework
- reusable in any kind of environment
- in particular testability in unit / integration
tests - Generic middleware services
- e.g. declarative transactions for POJOs
- flexible alternative to EJB CMT
- for all applications, including standalone
- leverage J2EE container services when available
8Spring Framework (3)
- Integration with existing solutions
- Object/Relational Mapping tools
- web frameworks
- remoting protocols
- "Its all about choice"
- JDBC, Hibernate, JDO, Oracle TopLink,Apache OJB,
iBATIS SQL Maps - Spring Web MVC, Spring Web Flow,Struts, WebWork,
Tapestry, JSF - HTTP invoker, RMI invoker, conventional
RMI,JAX-RPC (WSDL/SOAP), Hessian, Burlap
9Spring and J2EE (1)
- J2EE provides standard system services
- to be leveraged by higher-level components
- Spring abstractions can run on top of J2EE
Application components
Spring application containerSpring service
abstractions
J2EE system servicesJ2EE deployment and
management
10Spring and J2EE (2)
- Spring is a de-facto standard Java / J2EE
application framework - typically running on top of J2EE server
- but application components are not tied to J2EE
- Most popular "lightweight container"
- widespread adoption over the past 2.5 years
- endorsed / supported by BEA, IBM, Oracle
- e.g. support partnership for Spring on WebLogic
- EJB3 specification will follow Spring model
- adopts some important ideas from Spring
11Spring and J2EE (3)
- Constantly increasing download numbers
- 30.000 downloads of every point release
- gt400.000 downloads overall
- Growing Spring ecosystem
- Spring sister projects
- Acegi Security, Spring Web Flow
- used or supported by many other products
- open source and commercial
- e.g. Atlassian Confluence, Liferay Portal
- 5 dedicated books on Spring already available
- by various authors
- more books in the works
12Core Container (1)
- "Inversion of Control"
- configuration and lifecycle of application
objects - objects do not configure themselves, but get
configured from the outside - objects don't know the origin of their
configuration - "Dependency Injection"
- "setter-based" (JavaBean properties)
- "constructor-based" (constructor arguments)
- alternative "Service Lookup"
- for example JNDI
13Core Container (2)
- Fine-grained externalized configuration
- representing the internal structure of the
application - references to other components
- configuration parameters
- enables flexible configuration management
- at fine-grained component level
- switching between different deployment scenarios
- XML bean definitions
- most common configuration format
- often separate admin properties file
- linked into XML bean definitions through
placeholders
14Core Container (3)
ltbean id"dataSource" class"org.apache.commons.db
cp.BasicDataSource"gt ltproperty
name"driverClassName" value"jdbc.driver"/gt
ltproperty name"url" value"jdbc.url"/gt
ltproperty name"username" value"jdbc.username"
/gt ltproperty name"password" value"jdbc.passw
ord"/gt lt/beangt ltbean id"itemDao"
class"org.springframework.samples.jpetstore.dao.i
batis.SqlMapItemDao"gt ltproperty
name"dataSource" ref"dataSource"/gt ltproperty
name"sqlMap" ref"sqlMap"/gt lt/beangt ltbean
id"petStore"class"org.springframework.samples.j
petstore.domain.logic.PetStoreImpl"gt ltproperty
name"orderDao" ref"orderDao"/gt ltproperty
name"itemDao" ref"itemDao"/gt lt/beangt
15AOP Framework (1)
- "Aspect-Oriented Programming"
- proxies for arbitrary POJOs
- flexible combination of interceptors
- no fixed component model ("EJB a la carte")
- "Cross-Cutting Concerns"
- actions at the beginning/end of a method call
Target method
Caller
intercept
16AOP Framework (2)
- Do not repeat code factor out interceptor
- e.g. logging configurable trace log
- e.g. security authorization checks
- e.g. common exception handling
- e.g. transaction demarcation
- Method interceptor
- interceptor can be applied to any methods
- interceptor can be enabled/disabled
- AOP Alliance MethodInterceptor interface
- reuse of pre-built interceptors
17Transactions DAOs (1)
- Transaction Strategy Abstraction
- PlatformTransactionManager SPI
- switching between JTA and native transactions
- Transaction Demarcation Options
- programmatic demarcation a la JTA
- declarative demarcation for arbitrary POJOs
- Transaction Definitions
- all EJB CMT propagation codes supported
- REQUIRED, REQUIRES_NEW, etc
- optional transaction semantics beyond EJB
- nested, isolation level, timeout, read-only flag
18Transactions DAOs (2)
- DataAccessException hierarchy
- independent of JDBC, Hibernate, JDO, etc
- unchecked, as most failures are not recoverable
- subclasses like OptimisticLockingFailureException
- Support for DAO implementations
- implicit access to resources
- many operations become one-liners
- no try/catch blocks anymore
- Pre-built integration classes for many solutions
- JDBC JdbcTemplate
- Hibernate HibernateTemplate
19Transactions DAOs (3)
- Example for a JDBC-based DAO
- public class ExampleJdbcDao extends
JdbcDaoSupport public void clearDatabase()
throws DataAccessException     getJdbcTemplate()
.update("DELETE FROM imagedb")Â Â Â Â public
void deleteImage(int imageId) throws
DataAccessException     getJdbcTemplate().update
("DELETE FROM imagedb WHERE id?", new
Object new Integer(imageId))Â Â Â Â public
int getNrOfImages() throws DataAccessException
   return getJdbcTemplate().queryForInt(
"SELECT COUNT() FROM imagedb")Â Â
20Remoting
- Export POJOs as remote services
- in server-side Spring applications
- through Spring remote service exporter
- Make remote services accessible
- in client-side Spring applications
- through Spring remote proxy factory
- Protocol choice is configuration matter
- Hessian, Burlap, SOAP, RMI, HTTP invoker
- protocol to be chosen according to requirements
- Support for integration of EJBs
- through Spring EJB proxy factory
- declarative proxies for Stateless Session Beans
21Further Services
- JMS Support
- lightweight messaging
- JCA Support
- access to J2EE Connectors
- Mail Support
- JavaMailSender
- Scheduling Support
- Quartz, Timer
- Web Application Support
- Struts, JSF, WebWork, Tapestry
- Spring Web MVC, Spring Web Flow
22Summary (1)
- Spring is a popular Java application framework
- core container, AOP framework
- transactions, data access, remoting
- dedicated support for J2EE environments
- integrating with many existing solutions
- Solves ubiquitous architectural issues
- wiring and configuration of components
- flexible configuration of interceptors
- declarative transaction demarcation
- implicit management of resources
23Summary (2)
- Works in any environment
- no special compilation or deployment steps
- no special class loader
- can run on J2EE, but not tied to J2EE
- seamless switching between deployment scenarios
- For any kind of application
- J2EE web applications running on e.g. Tomcat
- full J2EE applications running on e.g. WebLogic
- rich clients (usually with J2EE server as
backend) - standalone applications (with GUI or headless)
24http//www.springframework.org
http//www.springframework.com