starcorn
starcorn

Reputation: 8551

Java dependency injection problem

I'm trying out some j2ee features which comes with Java reflection. Therefore i tried out the JNDI lookup which works without any problem, but I also wanted to try the opposite version "Dependency Injection". However it didn't work so well, and I couldn't find the reason for why either.

Anyway below it is just a simple function, which I just wanted to use to try out the DI feature. However when I compile it now I will get Exception in thread "main"java.lang.NullPointerException. Anyone can see where I've made wrong? Btw I'm using Glassfish with it. So have deployed a jar file in the /<glassfish>/autodeploy folder

The interface

import javax.ejb.*;
@Remote
public interface Hello {

    public void sayHello(Stiring name);
}

The implementation

import javax.ejb.*;

@Stateless(name = "FooBean", mappedName = "ejb/FooBean")
public class HelloImpl implements Hello {


    @Override
    public void sayHello(String name) {
        System.out.println("Hello " +name);
    }
}

The client

import javax.ejb.EJB;
public class ClientTest {
    @EJB
    static Hello bdb;

    public static void main(String args[]) {
        bdb.sayHello("starcorn");
    }
}

Upvotes: 1

Views: 1836

Answers (5)

starcorn
starcorn

Reputation: 8551

Hey I got the problem solved. Basically I could not run dependency injection as an normal java application. Therefore I had so solve by using Glassfish's appclient.

The things that I needed to do was to pack my Client with my EJB (FooBean) into an EAR project. And then generate a Jar file out of it. Beside that I also had to specifically at @EJB rewrite it to @EJB(name="beanName", unitName="beanUnitName") more over when I created the EAR jar, I had to specify where the Main class is located.

Upvotes: 0

Preston
Preston

Reputation: 3271

You can only inject into a managed object. For example a stateless / statefull session bean. In this case it looks like your "client" is not a managed object.

If you really want to go this route you might want to look into spring which will allow you to inject into non managed objects.

Upvotes: 1

Brett Kail
Brett Kail

Reputation: 33956

You cannot run a main class directly through Eclipse and still get dependency injection. You must use an application client container in order to get injection.

Upvotes: 0

JohnKlehm
JohnKlehm

Reputation: 2398

Not sure about J2EE specific but from general Java-ness you never initialize bdb (bdb = new HelloImpl()). So it seems like you're trying to call a non-static method on an uninitialized variable. If you make sayHello static you wouldn't need to initialize it (or even declare it as a member).

public class HelloImpl implements Hello {
    @Override
    public static void sayHello(String name) {
    }
}

public class ClientTest {
    public static void main(String args[]) {
        HelloImpl.sayHello("starcorn");
    }
}

Upvotes: 0

fvu
fvu

Reputation: 32983

You may have missed some steps along the road, hard to say with just the above fragments.... Have a look at this tutorial on the Netbeans site, it shows the complete procedure.

Upvotes: 0

Related Questions