|
Comments
|
Today's Top SOA Links
Enterprise Spring and Java EE 5 (PART 2)
Simplicity and power combined
By: Debu Panda
Jun. 26, 2007 09:15 AM
Register for Real-World Java Seminar In the first part of this article you learned how Java EE 5 has simplified enterprise application development by adopting a POJO programming model and making use of Java 5 metadata annotations. You also discovered how the Spring Framework version 2.0 integrates with Java Persistence API (JPA) and makes it simple to use from enterprise Java applications.
Using Spring and EJB 3 You can mix and match EJB 3 and Spring components to leverage the power of both frameworks. For example, you can combine such features as statefulness, pooling, clustering, declarative security, Web Service feature, and message-driven beans of EJB 3 with AOP, POJO injection , template-based data, and resource access support provided by Spring. Injecting Session Beans However you can use Spring to simplify and use its powerful dependency injection mechanism to inject an instance of a session bean. This will help your applications to port between application servers. Let us dive down and see an example. The code examples that follow are taken from my recently published book, EJB 3 in Action, published by Manning Publications. Assume that you have a session bean named ItemManager that you will use in the ItemServiceBean, which is a Spring POJO, as shown below: public class ItemServiceBean implements ItemService { As you can see, we are using setter injection to inject an instance of ItemManager EJB object and invoke a method on the EJB. By now you must be wondering where the actual magic happens? We're not doing a JNDI lookup and not using the @EJB injection (that we discussed in Part 1) to inject the session bean object. The real magic occurs through wiring in the EJB through the Spring configuration. Let's assume that the Spring Bean uses a remote business interface of ItemManager EJB and retrieves it using a Spring 2.0 simplified jee-jndi lookup as follows: <jee:jndi-lookup id = "itemManager" jndi-name = "ejb/ItemManager" resource-ref = "true"/> Note that we are using setter injection in ItemServiceBean to inject an instance of ItemManager EJB and we must wire the ItemManager EJB as follows: <bean id = "itemService" class = "actionbazaar.buslogic.ItemServiceBean"> Remember from our discussion in part 1 of the article that EJB 3 Session beans are POJOs and interfaces are POJI. So there's no difference between invoking EJB3 session beans with either a remote or local interface if your Spring beans and EJBs are collocated in the same container, and the configuration is identical for both local and remote session beans. If your Session bean is in a remote container you'll have to provide the JNDI properties such has provider URL, principal, and credentials to invoke the remote bean. Spring-Enabled Session beans Below is the same EJB 3 example (PlaceBid EJB) transformed into a Spring-enabled stateless session bean. Here the PlaceBid EJB acts as a façade and delegates the actual business logic to the PlaceBidServiceBean. @Stateless(name = "PlaceBid") Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||