You Don't have to Choose! Spring 3 and EJB 3 at DevIgnition

Reza Rahman is speaking at DevIgnition at Oracle HQ on Java EE 6 Support for Spring 3.

Reza has spoken at the NovaJUG in the past on Java EE 6 and there are previous posts on translucent development from his talks. He also has good taste in beer!

He is an Expert Group Member for Java EE 6 and EJB 3.1 and the author or EJB 3 In Action

Java EE 6/Spring 3

  • Significant release, by and large well received
  • Chopping dead wood, lightweight Web Profile
  • CDI, JSF 2, EJB 3.1, JPA 2, Servlet 3, JAX-RS, bean validation
  • Majority of Java EE APIs can be used with Spring
  • Java EE 6 support major theme of Spring 3 including bean validation, JSF 2, JPA 2, managed beans, JSR 330 and even more EJB 3 annotations

Reza really is saying that, Yes you can use Spring together with EJB 3 and it doesn't have to be an either/or decision!

JSR 330, JSR 250, and EJB 3 Annotations are supported in Spring

@Named, @Inject, @ManagedBean, @PostConstruct, @PreDestroy, @Resource, @EJB, @TransactionAttribute, @Asynchronous, @Schedule

These are examples are what you can use from the JSRs with Spring 3.


JSF widely used in the Spring community, likely to increase adoption with JSR 2 and Facelets

  • Spring beans can be injected into JSF Managed Beans
  • Spring beans can be referenced in EL with no JSF Backing Beans
  • Several resolvers available depending on usage pattern

** BTW, Reza's "I see Dead Code" shirt is kinda cool **

* Showing action code, configs, xhtml files in Eclipse *

Going into the EL resolver: SpringBeanFacesELResolver from Spring

Spring Plus Bean Validation

  • Allows for declarative validation constraints across your application
  • Usually sued through JSF 2 and JPA 2 without any Spring configuration
  • Spring can be used to bootstrap/inject Validator or use Bean Validation with Spring MVC instead of JSF 2

* Going back to the demo to show the validation in use *

"Username must be between 7 and 30 characters"

You can control how that error message looks with CSS. By default it is kind of ugly, and just shows it at the bottom of the UI form.

Annotations such as: @NotNull, @Size can be used including specifying the error message, and can load those up from an international (i19n) properties file.

Question: Can you control how much is validated? Such as if a username isn't entered, don't validate the password:

Yes and No. There are ways such as a custom event handler.

Can the rules be put into an external file?

You can use XML. He believes that the XML is bound to a particular bean.

Spring Plus JPA 2

  • JPA is popular with newer Spring projects, JPA 2 is likely to increase adoption further
  • Several JPA support configurations possible - Java SE bootstrap, Java EE container managed and Spring manage
  • Most JTA implementations use local transactions by default and transparently upgrade to XA when actually needed
  • Application server data sources use connection pooling, prepared statement caching, debugging, monitoring and security

* going through applicationContext.xml, applicationContext-persistence.xml, and applicationContext-messaging.xml

Spring Plus JAX-RS/JAX-WS

  • Spring has Spring MVC based non-standard REST API
  • JAX-RS has better adoption in the Spring community
  • Jersey, RESTEasy, and Apache CXF include integration support for Spring
  • JAX-RS via EJB 3/Spring integration minimizes needless configuration
  • Spring/JAX-WS integration very similar
  • Spring has JAX-WS exporters

You can bootstrap the SpringServlet in your web.xml

Spring Plus EJB3

  • EJB 3 beans transactional, secure, pooled, thread-safe, bandwidth-throttled, and closely manage by the application server
  • Can utilize passivation, caching, clustering, load-balancing and remoting for greater scalability
  • is well-equipped for the service tier of non-trivial applications but is overkill for most other components
  • Streamlined EJB 3 component model approaches zero-configuration systems with intelligent defaults
  • Spring 3 supports excellent bi-directional integration with EJB 3

EJB 3 Gives you a lot if you need it. Use it when you need these things!

A little magic in the file beanRefContex.xml and is with your other Spring configuration files.

Spring Plus JMS

  • Spring JmsTemplate can be used on top of raw JMS API for ease-of-use (with some careful considerations)
  • Spring listeners roughly equivalent to EJB 3 Message Driven Beans, especially JCA rather than JMS listeners
  • Transactions/XA, acknowledgment, pooling, parallel processing not provided by default!
  • EJB 3 can be used for better reliability and scalability

Spring on Tomcat with Java EE APIs

  • JSF 2, Bean Validation, JPA 2, JAX-RS and JAX-WS can easily be embedded into Tomcat
  • Pluggable JMS implementation
  • Spring provides some replacement services for transactions, database connectivity, thread-pooling
  • Embedded EJB 3 containers like OpenEJB can be integrated into Tomcat to provide EJB 3, JTA/XA

Create a powerful super-stack that utilizes the strengths of both Spring and Java EE

Use Java EE as the runtime for standardizations, ease-of-use, administration and scalability while Spring is used for flexibility and third party integration as well as preserving existing development investment/knowledge-base

Great Talk as usual!

Digg this

Theme provided by Danetsoft under GPL license from Danang Probo Sayekti