Jin Kwon
Jin Kwon

Reputation: 22027

EJB as an Interface is not injected

I defined an interface.

@Local
public interface MessageService {
}

And an implementation.

@Stateless
public class MessageServiceSome implements MessageService {
}

When I tried to inject it into my resource class, I got null.

//@Path("/messages") // I'M NOT GOING TO MAKE THIS STATELESS!!!
public class MessagesResource {

    // none of follwoing options works
    // leaves the field null
    @Inject
    //@EJB
    //@EJB(beanName = "MessageServiceSome")
    private MessageService messageService;
}

How can I solve this?

UPDATE

I think I have to admit that my question is not good enough.

The MessagesResource class was actually a sub resource. I didn't know the difference.

There are two very good threads for this issue.

  1. https://stackoverflow.com/a/36291890/330457
  2. https://stackoverflow.com/a/24670218/330457

One is using ResourceContext and the other is using Inject.

Both threads are saying they work but I only succeeded with @Inject.

Upvotes: 1

Views: 981

Answers (1)

Leonardo
Leonardo

Reputation: 9857

With little information provided, you have probably two quick options you can try:

  1. leave @Inject only, if your project/container is CDI enabled

    @Inject private MessageService messageService;

  2. leave @EJB only, do you really need the beanName ?

    @EJB private MessageService messageService;

on of the two should solve the issue.

[UPDATE] Otherwise have a look at the app server start up log, and see if the bean has been deployed.

Upvotes: 2

Related Questions