Dependency injection between two local EJBs with Netbeans

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: