The sound of the letters E-J-B make many developers cringe. These three little letters bring back painful memories of home interfaces, deployment descriptors, java.rmi.RemoteException and of course, most painful or all... entity beans.
But there's good news on the horizon. The final release of the EJB 3 spec is just around the corner and thankfully much of the pain is now a thing of the past. I'm not saying it's perfect, but it's a huge improvement over the current disaster of EJB 2.1. Ok, perhaps "disaster" is a little harsh, but I think you'd have a hard time finding a developer who likes to program EJBs in their current state.
So what's new in the 3.0 release. Well a quite a lot actually. Here is a list of the more interesting changes...
- Everything's a POJO. At last, no more "extends EJBHome" or "implements SessionBean". The new spec allows any plain old java object to be enterprise beans. This is a welcome change for those of us who like to unit test our EJBs and don't like the messy test frameworks that the current spec forces us to use.
- Annotations are the new deployment descriptor. One of the more controversial new features in Java 5.0 is annotations. Love them or hate them they're here to stay. And the new EJB spec has embraced them with open arms and made them an integral part of the framework. Now you can do away with those ugly xml deployment descriptors and use annotations instead. However, if you want to continue to use deployment descriptors then that's fine too. In fact, you can even use a combination of the two.
- Configuration by exception. Another welcome change. No longer do you need to configure every last option of every piece of your application. Sensible defaults are available for most things and you need only define what you want to change.
- Dependency Injection. EJB has jumped on the dependency injection bandwagon and now provides some basic injection functionality. You can have JNDI resources and other beans injected for you at runtime, saving you the trouble of looking them up or creating them yourself.
- No more home interface. I don't think anyone will miss them.
- Simplified Persistence. Those nasty entity beans aren't so nasty anymore. For starters they can be POJOs now, and you can map them using simple annotations. Entity beans are much nicer in general really. But don't get too excited, Hibernate is still better.
- AOP support (sort of). With the introduction of interceptors, EJB now lets you do some very basic aspect oriented programming. It's not fantastic, but it works and it's better than not having it all. Basically you can specify that a method on an interceptor class be called whenever a business method is called on your EJB. However, it is somewhat restricive in the way it works. It would be useful for generic stuff like logging or audit information, but it would be a little clumsy for anything too complex.
- Callbacks. You can now define methods on your EJBs that will be notified of lifecycle events. By simply annotating a method as a callback method it will be notified of various events (there are several, depending on the type of bean).
- Fewer checked exceptions. The question of checked vs unchecked exceptions can quickly become a heated debate. But I think it's a great idea. It removes those unecessary try/catch blocks and throws clauses and makes code so much cleaner. There's not much you can do with a checked exception at that level anyway. But if you feel you need to catch them then you still can.
- Interoperability. The good news is that your old EJBs have not been rendered useless. You can still use them in conjuction with the EJB 3.0 framework. So if you have old legacy EJBs that you just can't live without, never fear, you don't have to re-write them just yet.
As you can see, there's lots of new stuff. Over the next few weeks I'll discuss all of these new features in all their gory detail. In the next article I'll discuss the changes that have been made to our good friend - the session bean.