November 23, 2014
Hot Topics:

Synergy Between CDI and EJB 3.1, Page 2

  • April 25, 2011
  • By Sangeetha S
  • Send Email »
  • More Articles »

Injecting Beans

CDI services allow beans to be generally injected into another application using JSR 330, Dependency Injection for Java specification. The annotation @Inject part of JSR 330 allows beans to be injected. This annotation helps to inject any object for which the container provides the required resource.

@Named
@Stateful
public class Login {
@Inject
private String uname;
@Inject
private String pwd;
….
}

CDI services come with other interesting features like events, interceptors, decorators etc. Since the discussion on these are beyond the scope of this article, readers are requested to learn more about CDI services from the JSR 299 and other articles which focus completely on the CDI services.

Enterprise JavaBeans EJB 3.1: A Brief Overview

EJB 3.0 specification, the version currently in use, has seen higher adoption rate and was very well received by the community because of its simplicity and ease of development. The new specification that was released in November 2009, EJB 3.1, makes development even simpler.

EJB 3.0 made the developer life easier by removing home interface and introducing annotations. The next release makes life even easier by making the business interfaces also optional. This makes the EJBs look very similar to a normal POJO.

Addition of a new type of bean, Singleton bean is introduced in this release. The primary goal of introducing singleton beans is to share the application wide shared data and to support concurrent access. With this, the EJB specification has provided a flexible concurrent option.

Until now, EJB components are placed in a separate JAR file and the file will be packaged along with WAR file in an EAR file. One of the major requirements of the new specification is providing an option to package EJBs in the WAR file itself, thus making EJBs to be available as part of the Web application with all required support for transactions and security. The new specification allows EJBs to be placed in the same folder where the Web component classes (Servlets) are placed, under WEB-INFclasses directory of a WAR file.

@Stateless
public class Stock {
public double getStockPrice(String symbol) {
if (symbol.equals("PQRS")) {
return 234.98;
}
if (symbol.equals("OPLK")) {
return 64.45;
}
return 0.0;
}
}

Apart from these, the new release of the specification introduces several other interesting features like asynchronous session bean, running EJBs in Java SE environment and the concept of EJB Lite.

EJB provides a powerful support for transactions. Transactional attributes can be defined using annotations thanks to the "Configuration by Exception" model adopted from Java EE 5 Platform. Annotations play a major role in simplifying and making the entire suite of specifications light.

CDI and EJB: Complementary Technologies

EJBs are really simple and powerful after adopting the POJO model. Even though it provided a good support for transactions and security, one particular place where it lacked was the direct usage of it with the JavaServer Faces application. In JSF application, there was a need to create managed beans separately in spite of EJB being so powerful. On the other hand, simply beans are not powerful. They have to be executed under some contexts. CDI services really help in bridging the gap between the two; it helps in EJB getting directly exposed to JSF applications. In this scenario, CDI becomes the complementary technology for EJB 3.1 specification in the Java EE 6 platform.

In enterprise applications using EJBs, CDI services allow enterprise beans to be injected as Managed Beans with the @Inject annotation by removing @EJB annotation. The replacement of @Inject annotation provides all the benefits of CDI for the enterprise beans like dependency injection, events, interceptors, stereotypes. Thus using @Inject annotations, enterprise beans can be easily transformed to managed beans.

An enterprise bean represented like the one mentioned below:

@Stateless
public class SQBean {
@EJB
private StockBean stockBean;

@EJB
private Stock stockPrice;

public void getStockPrice() {
String symbol= stockBean.getStockSymbol();
double price = stockPrice.getStockPrice(symbol);
}
}

Can be easily transformed into a Managed Bean like this:

@Named
@Stateless
public class SQBean {
@Inject
private StockBean stockBean;

@Inject
private Stock stockPrice;

public void getStockPrice() {
String symbol= stockBean.getStockSymbol();
double price = stockPrice.getStockPrice(symbol);
…
}
}

This transformation of enterprise bean to a managed bean provides lot of benefits for the Web tier components and applications. The beans are now visible to the Java Server Pages; they can be accessed using the Expression Language (EL). The class SQBean is now visible to JSP, JSF expressions. The @Named annotation is used for associating a name with the managed bean, if none specified, by default the name will be the name of the bean with the first letter in the lowercase. This facilitates the SQBean to be accessed directly by the EL in JSF pages without any additional configuration.

….
<h:inputText id="stockSymbol" value="#{stockBean.symbol}" />
<h:commandButton value="Get Stock Price" action="#{sQBean.getStockPrice}" />
….

As mentioned earlier, CDI also believes in convention over configuration, and hence doesn't require any additional XML files for configuration; annotations are sufficient. CDI helps to inject any kind of object be it a class or an interface, only the type of the object being injected is checked. This is complement to the EJB model where the object to be injected is identified by a "String" name as against the type of the object being injected. CDI also to some extent supports identifying the injection object with string name, this time it is through its @Qualifier annotation.

@Qualifier
@Retention(RUNTIME)
@Target({FIELD,TYPE})
public @interface CheckUser {
   	 enum User{
    	 VALID, INVALID
    	}
    	User details();
}

At the time of specifying the injection point, qualifier helps in identifying the type of object being injected.

@Named
@Stateless
public class Login {
…
@Inject
@CheckUser(CheckUser.details.VALID)
private String username;
…
}

Annotations support makes convention over configuration more beneficial. XML configuration is not completely ruled out; it is still available as an "optional" feature through "beans.xml" configuration file. The injection object can be activated from the XML file. @Alternative annotation plays a major role in activation and deactivation of the injection points. Another feature which is interesting in CDI is event firing and observation which is again achieved through the methods and annotations.

Conclusion

CDI and EJB 3.1 make the perfect complementary technologies as EJB comes with good support for transactions, security and exiting features like interceptors and aspects. CDI provides additional power of dependency injection. The combination of the two provides a perfect synergy in the Web platform. CDI with EJB 3.1 makes the code lighter because they are simple POJO classes, when compared with plain POJOs or any other additional frameworks, the number of code written using CDI and EJB 3.1 is less. Frameworks will end up in either more classes or more configuration code, XML files. Both CDI and EJB 3.1 are part of Java EE 6 Platform - Web Profile, providing a good strategy for building Web applications which also results in reduced number of classes and code as compared to its earlier versions. So in future, the JCP could be more focusing on pushing aspects of transactions, security, aspects into CDI specification rather improving it on the EJB specifications.

Acknowledgements

The author would like to sincerely thank Mr. Subrahmanya SV, VP, ECOM Research Group, E&R for the idea, guidance, support and constant encouragement and Mr. Nitin KL for quickly reviewing this article.

About the Author

Sangeetha S. works as a Senior Technical Architect at the E-Commerce Research Labs at Infosys Technologies. She has over 12 years of experience in design and development of Java and Java EE applications. She has co-authored a book on J2EE architecture and also has written numerous articles for Developer.com and DevX.com. She can be reached at sangeethas@infosys.com.


Tags: Java EE 6, EJB, CDI

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel