Dependency injection between two local EJBs with Netbeans

7 07 2008

This a simple tutorial about how to create a local dependecy injection within two EJBs 3.0.

The point is creating one EJB that calls a method from another local EJB with Netbeans and Glassfish.

Start Netbeans and create a new EJB project called Util and add a new session bean to it. Make it stateless and local.



@Local
public interface UtilLocal
{
    int returnInt();
}

@Stateless
public class UtilBean implements UtilLocal
{
    public int returnSomething()
    {
        return 0;
    }
}


Now create a second EJB project called Main and add a new session bean to it, like in the Util EJB. Include the Util EJB inside the Main EJB project.



@Local
public interface MainLocal
{
    int doSomeLogic();
}

@Stateless
public class MainBean implements MainLocal
{
    @EJB(beanName="UtilBean")
    UtilLocal utilEJB;

    public int doSomeLogic()
    {
        return utilEJB.returnSomething();
    }
}


Note that we use @EJB to instantiate the Util EJB object.


Simple yet effective :mrgreen:



Actions

Informations

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image