| By Debu Panda | Article Rating: |
|
| June 26, 2007 09:30 AM EDT | Reads: |
33,394 |
Dependency Injection
With Java EE 5, invoking an EJB component is a snap with dependency injection. Dependency injection support in Java EE 5 was influenced by inversion of control (IoC) containers such as Spring.
Dependency injection is essentially the opposite of the JNDI: the container is responsible for looking up and instantiating an instance of an EJB or a resource. Figure 1 depicts how dependency injection compares with JNDI.
For example, in Figure 1 you can invoke the EJB from a managed component as follows:
@EJB
private PlaceBid placeBid;
..
Long bidId = placeBid.addBid(bid);
..
Although Java EE 5 simplifies usage resources and services by using dependency injection, it is supported only on managed classes such as Java servlets, listeners, and EJBs, and not regular Java classes. Later I'll discuss how you can use Spring's powerful dependency injection features to fill that gap.
Note that Listing 2 uses JMS Queue to send messages. The @Resource annotation injects an instance of JMS connection factory and destination, respectively, without having to do a complex JNDI lookup. Usage of JMS is fairly complex, but Spring simplifies it with its JmsTemplate, as we will see in the second part of this article.
Simplified Web Service
Development of Web services was incredibly complex with the Java API for XML-based RPC (JAX-RPC), which was part of earlier versions of J2EE. The Java API for XML Web services (JAX-WS), part of the Java EE 5 platform, simplifies the development and invocation of Web services by using annotations. You can easily expose any POJO or stateless EJB as a Web service by using the @WebService annotation. For example, you can expose the PlaceBid EJB as shown in Listing 3.
If you have used Web services in J2EE 1.4, you'll realize how simple it is to develop Web services in EJB 3!
The invocation of Web services was also a complex task with J2EE 1.4. Java EE 5 simplifies it by using @WebServiceRef annotations, as shown here:
@WebServiceRef(wsdlLocation="http://localhost:8888/PlaceBidService/PlaceBidService?WSDL")
private static PlaceBidService placeBidService;
actionbazaarplacebidservice.PlaceBidBean placeBid = placeBidService.getPlaceBidBeanPort();
System.out.println("Bid Successful, BidId Received is:" +placeBid.addBid
("idiot", Long.valueOf(1), 2000005.50 ));
Again, however, @WebServiceRef injection is only supported with managed classes and cannot be used from regular Java objects.
In the first half of the article, I discussed how Java EE 5 simplifies development of enterprise Java applications. Now I'll explore how you can use Spring to address limitations in Java EE.
Integrating the Power of Java EE 5 and Spring
The Spring Framework provides a lightweight container with functionality that simplifies the development of applications. Although many view Spring as an alternative to Java EE, most customers use it as a framework and deploy Spring-based applications into a Java EE container such as Oracle Containers for J2EE (OC4J).
Spring simplifies resource access by using dependency injection, and simplifies database access by using a template-based approach. In this section, I'll take a look at these capabilities.
JPA and Spring
Spring has wide support for data access that includes popular object-relational mapping (ORM) frameworks, including Hibernate and TopLink. Spring takes a data access object (DAO) approach to coding, allowing developers to use these ORM options and switch between them easily. While JdbcTemplate makes database access using JDBC simpler, Spring 2.0 has built-in integration with JPA, and it ships TopLink Essentials (the reference implementation of JPA derived from Oracle TopLink in Sun's GlassFish project) as the default JPA provider.
While Spring supports container-managed EntityManager as a lightweight container, it simplifies usage of JPA with JpaTemplate. Table 1 shows several Java interfaces for using JPA in Spring.
Spring enables you to access and manipulate entities either using JPA directly or using JpaTemplate. I will now demonstrate how to use JPA from applications using JpaTemplate. Listing 4 shows a DAO implementation class using JpaTemplate.
You can use the JpaTemplate methods to access entities. Spring really simplifies some of the repetitive usage of EJB 3 JPA. For example, if you want to use a dynamic query to retrieve all bids for an item, you can use the JpaTemplate to produce this:
List bids = getJpaTemplate().find("SELECT b FROM Bid b WHERE b.item = ?1",item);
The equivalent code, if you use the EntityManager API, will look like this:
List bids = em.createQuery("SELECT b FROM Bid b WHERE b.item = ?1")
.setParameter(1,item).getResultList();
The real power of Spring comes from how it configures services via dependency injection. To use JPA with Spring, you need the configuration shown in Listing 5.
JPA is not the only cool thing about EJB 3; there is more meat in it, and there are more integration points between Spring and EJB 3 that we will discover in the next part of this series.
Conclusion
In this article, you learned how Java EE 5 simplifies the development of enterprise-level applications. You saw examples of JPA entities, EJB 3 session beans, and a simple JAX-WS Web service. All these components are POJOs and make heavy use of annotations. JPA greatly simplifies the building of persistence applications.
The Spring Framework 2.0 has not only integrated with JPA, but also greatly simplifies usage of JPA by using JpaTemplate. In the next part of the series we will discover how the Spring Framework integrates with other components such as an EJB, Java Message Service (JMS), and transaction manager. Stay tuned!
Published June 26, 2007 Reads 33,394
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Debu Panda
Debu Panda, lead author of the recently published EJB 3 in Action (Manning Publications), is a senior principal product manager on the Oracle Application Server development team, where he drives development of the Java EE container. He has more than 15 years of experience in the IT industry and has published numerous articles on enterprise Java technologies and has presented at many conferences. Debu maintains an active blog on enterprise Java at http://www.debupanda.com.
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Industry Experts Discuss the State of Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- It's the Java vs. C++ Shootout Revisited!
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer Aid Campaign for the Typhoon Ondoy Victims
- Cloud Computing Can Revitalize Your Career as Software Developer
- A Brief History of Cloud Computing
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Industry Experts Discuss the State of Cloud Computing
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Performance Tuning Essentials for Java
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- My Thoughts on Ulitzer
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?































