How to configure EJB 3.0 Session Beans using XML (sample)

EJB 3.0 Session Beans can be configured using XML in different ways. You can use dependency injection, as well as programmatic lookup for this purpose. The dependency injection approach allows you the definition of default values in the bean class, which can be overwritten in the deployment descriptor (the XML-file).

All fields, which are going to be injected from XML should be marked with the @Resource tag. The default value can be already specified in the declaration.

public class ConfigurableBean implements Configurable {

    @Resource int someIntInjectedValue = 100;
    float someFloatValue = 2;
    @Resource SessionContext sessionContext;
    public int getSomeIntInjectedValue() {
        return this.someIntInjectedValue;
    public float fetchSomeFloatValue(){
        return (Float)this.sessionContext.lookup("someFloatValue");

 The desired value has to be specified in the XML deployment descriptor.



The <injection-target> tag has to be specified as well. It causes the actual injection. However, this is not DRY (the FQN of the bean appears in the XML again) and somehow annoying. However, we are going to optimize it in EJB 3.1...:-)



 The traditional "lookup" of environment entries can be still performed. However the injection of the SessionContext is needed for this purpose... I checked in the whole sample project into P4J5 (name: SessionBeanConfiguration).


I'm sure the developers and designers of Glassfish are all among the finest of designers/developers and all that, but I have to say this : I am just floored at the ***massive*** effort it takes to get anything nontrivial done in glassfish.

EJB3.0 is mostly a curse - it was meant to simplify things but has instead turned into a Pandora's box of an insane array of ways of doing things.

At least with DD there were XML-schemas and you could know when it was wrong. Annotations? Glassfish just breezes by silently ignoring and/or failing.

I'm starting to long for the DD-only days

Posted by Paul on August 18, 2010 at 02:48 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments