Jonathan S. Fisher
Jonathan S. Fisher

Reputation: 8876

Does CDI offer an api for a default producer?

I have a bunch of dependencies written as fast binary web services (aka Ejb3.1). Here is the service delcaration:

@Remote
public interface MyService {...}

You would inject an EJB into a servlet or managed bean with the following syntax:

@EJB
MyService myService;

I don't want to use the @EJB injection however. I'd like to use plain vanilla CDI:

@Inject
MyService myService;

One way to accomplish this would be to Create a @Produces method for every EJB:

@Produces MyService produceMyService(InjectionPoint ijp){
 //jndi lookup for MyService interface
}

However, InjectionPoint is capable of giving you all the information you need, such as the target class name (MyService in this case).

Is there a way in CDI to do something like this? I'd want to call this producer last, if the required injection point couldn't be fulfilled in any other manner.

@Produces Object produce(InjectionPoint ijp){
 Class ejbInterface = ijp.getType();
 //jndi lookup for ejbInterface
}

This is a confusing post, so ask clarification questions. Thanks a ton!

Upvotes: 0

Views: 777

Answers (1)

Jan Groth
Jan Groth

Reputation: 14656

Assuming that I understood your question (see comment): No, there is no API for this.

Good news is that there is a way to achieve this - but you probably don't want to do this at runtime, that's rather a task for application startup.

The CDI extension mechanism offers you some well defined hooks into bean processing at container startup. This is a perfect place for logic that decides about enabling / disabling of certain managed beans (probably based on static classpath information).

Have a look at function and implementation of Seam Solder's @Requires. That should be pretty close to your use case...

Upvotes: 3

Related Questions