| Sign In/My Account | View Cart |
Using Dependency Injection in Java EE 5.0
Pages: 1, 2, 3, 4, 5
Dependency injection makes resources and services easier to use because you don't have to deal with the complexities of JNDI. You don't have to write hundreds of lines of code or write service locator patterns.
Java EE supports dependency injection either via XML or annotation, so use annotation only when it makes sense. Annotation can make your code more readable and concise--not to mention make your life easier--but it can also cause maintenance problems, since the method requires that resource references are hard-wired into the application code. The good news is that you can override annotations using XML elements.
Because Java EE 5.0 supports dependency injection with managed classes, you can't use dependency injection from helper classes. And you must still use JNDI if you want to use resources or services from a helper class.
Dependency injection will greatly simplify the complexities of JNDI and make it easier to develop enterprise applications. See for yourself how it makes using resources and services easier: try it out in Oracle Application Server 10g 10.1.3 and JBoss 4.0.x, both of which offer early support for EJB 3.0 and dependency injection in the EJB container.
Debu Panda is a Senior Principal Product Manager of the Oracle Application Server development team.
Return to ONJava.com.
Showing messages 1 through 13 of 13.
Well this article is my first encounter with the new EJB specs and it is rather early in the morning so I might miss some important issues here but basically I don't see any difference between
Context ic = new InitialContext();
HelloWorld helloWorld = ( HelloWorld)ic.lookup("java:comp/env/ejb/HelloWorld");
and
@Resource(name="ejb/HelloWorld")
private HelloWorld helloWorld;
Okay, it looks different and you don't seen any InitialContext magic but to me it is still the same, this is not inversion of control as I appreciate it from Spring.
For me the prime reason to use IOC is that in a certain location I want to use an object implementing a certain interface but without having to determine at that location what object (of what exact concrete type) I am going to use. This functionality is basically already provided in the old J2EE specs as you lookup an object by name in the J2EE context. It is in the J2EE config where you define 'the" object suited to become the object implementing the desired interface. The same type of object lookup is also available in Spring by using its ApplicationContext and looking up a POJO bean by name.
Still the problem here is that this is not really IOC, its is an enhanced version of an implementation of the Interface Design Pattern. The problem as I see it is that sometimes you suddenly need 2 different objects of different types implementing a certain interface instead of just one. And if you developed your application by looking up objects by their configuration name. Example: an application uses a datasource to retrieve and store data. The data source is retrieved from the runtime environment using a single name AdventureDB. But then for some architectural reason 2 different databases are put into use and thus 2 differently configured datasources with 2 different names. Ok, it is simple to alter the code to use a different name in the lookup process. But if you used real IOC this wouldn't be necessary.
This is my second reason to use IOC in its full glory: in a spring based application where I need a datasource I provide a setter method to inject a datasource and in my spring config I define for a lot of different objects which datasource they need to use. Okay okay, if one object suddenly needs to use 2 different datasources this scheme also needs modification then.
Maybe the @Resource javax.ejb.TimerService ts; is a J2EE example of a more complete IOC as I just described it. But then the article is missing one element: where do you define the resource ? E.g. where in the new J2EE do you define the real class of which the resource is going to be a runtime instance of?
And finally one last remark which is not the issue of the article "Using dependency injection", where in J2EE 5.0 can I find Aspect Oriented Programming, which is the third reason I like Spring way over J2EE 1.4.
On the whole: a nice article on J2EE 5.0 but regarding J2EE 5.0 itself: too little too late.
Just my $.02
For example, consider if I had the following class:
class DisplayMessage {
Message message;
Message setMessage(Message message) {
this.message = message;
}
Message getMessage() { return message; }
void displayMessage() {
... something complicated ...
}
}
Now perhaps I have many other classes that need DisplayMessage objects injected. Some with different Message values?
In Spring this is pretty easy. I just assign one resource name for each message object I will need, in inject the appropriate message.
For example:
<bean id="displayMessage1" class="Message">
<property name="message" ref="message1"/>
</bean>
<bean id="displayMessage2" class="Message">
<property name="message" ref="message2"/>
</bean>
<bean id="displayMessage3" class="Message">
<property name="message" ref="message3"/>
</bean>
However, with annotations the message is contained within the class itself as an annotation. If you want to actually have different object which only vary by what value is ejected into them, you actually have to make separate classes. How stupid is that?
A commonly used class might be replicated hundreds of times, just so different values can be injected.