One of the primary goals for J2EE 5.0 is to simplify J2EE development by using metadata annotations and it is evident from J2EE 5.0 Early Release Draft specification.
One of the primary complexities of J2EE comes from JNDI for using of EJBs, Resources and environments. Most of developers spend a lot of hours debugging issues surrounding to JNDI lookup. J2EE 5.0 (and EJB 3.0) promises to resolve this by introducing dependency injection mechanism for using resources such as DataSource, JMS, etc. and environment variables. Use of dependency injection will remove the need for complex JNDI lookup for getting EJB, resources and environments. Dependency injection can be used both by using deployment metadata annotations and deployment descriptors.
In J2EE 1.4, use of resources is really complex. For example, for using an EJB from another EJB required these steps.
1. Add ejb-ref in the deployment descriptor
<ejb-ref-name>MyCart</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>CartHome</home>
<remote>Cart</remote>
Look up and create an instance of EJB
Object homeObject = context.lookup("java:comp/env/MyCart");
CartHome home = (CartHome)
PortableRemoteObject.narrow(homeObject,CartHome.class);
Cart cart = (Cart)
PortableRemoteObject.narrow(home.create(), Cart.class);
cart.addItem("Item1");
In J2EE 5.0 (EJB 3.0), you can simple define the dependency as follows and invoke the methods directly:
@EJB CartEJB cart;
public void addItems() {
cart.addItem("Item1");
}
For using a JMS resource, we can inject the JMS resources as follows:
@Resource(name="jms/demoTopic")
private javax.jms.Topic demoTopic;
@Resource(name="jms/TopicConnectionFactory")
private javax.jms.TopicConnectionFactory topicConnectionFactory;
TopicConnection connection= topicConnectionFactory.createTopicConnection();
Let us take an example how you can use dependency injection with deployment descriptors.
1. Add the name of the field in which the environment variable by using injection target as follows:
<env-entry-name>FahrenheitString </env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>degrees Fahrenheit </env-entry-value>
<injection-target>fahrenheitString </injection-target>
2. Then declare the field in the class where it is being injected to as follows:
public class WeatherReportBean implements WeatherReport {
// Strings are configured for injection in the ejb-jar.xml file
private String fahrenheitString;
private String fahrenheitString;
Here is an odd thing that found. J2EE 5.0 simplifies Web services Metadata but interesting thing is that the J2EE 5.0 has no mention how to use annotations for web service references (service-ref element).
Looking at above examples it is evident that dependency injection will simplify use of resources, EJB from J2EE applications.
11:46:37 AM
|