Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CDI for method parameter injection?

Tags:

Is it possible to use CDI to inject parameters into method calls? The expected behaviour would be similar to field injection. The preferred producer is looked up and the product is used.

What I would like to do is this:

public void foo(@Inject Bar bar){   //do stuff }  

or this (with less confusing sytax):

public void foo(){   @Inject    Bar bar;   //do stuff }  

This syntax is illegal in both cases. Is there an alternative? If no - would this be a bad idea for some reason if it were possible?

Thank you

EDIT - I may have made my requirements not clear enough - I would like to be able to call the method directly, leaving the initialization of the bar variable to the container. Jörn Horstmann's and Perception's answer suggest that it is not possible.

like image 319
kostja Avatar asked May 23 '12 10:05

kostja


People also ask

Would you recommend to use CDI or spring annotations?

To summarize: CDI is nothing like a "replacement" for the Spring ecosystem, it's rather an improvement over Spring's dependency injection mechanism. It's part of Java EE 6, so if you are on a GlasFish with Java EE 6, you should definitely go for CDI.

What is the use of @inject annotation?

Injectable constructors are annotated with @Inject and accept zero or more dependencies as arguments. @Inject can apply to at most one constructor per class. @Inject is optional for public, no-argument constructors when no other constructors are present. This enables injectors to invoke default constructors.

How do you make a class injectable in Java?

In order to inject class it should be: Concrete class (i.e not abstract or interface) or it should annotated as @Decorator. Should have no-arg constructor or constructor annotated with @Inject. Should not have annotated with an EJB component-defining annotation or declared as an EJB bean class in ejb-jar.

What is a CDI application?

Overview. CDI (Contexts and Dependency Injection) is a standard dependency injection framework included in Java EE 6 and higher. It allows us to manage the lifecycle of stateful components via domain-specific lifecycle contexts and inject components (services) into client objects in a type-safe way.


2 Answers

Injection points are processed for a bean when it is instantiated by the container, which does limit the number of uses cases for method level injection. The current version of the specification recognizes the following types of method injection:

Initializer method injection

public class MyBean {     private Processor processor;      @Inject     public void setProcessor(final Processor processor) {         this.processor = processor;     } } 

When an instance of MyBean is injected, the processor instance will also be injected, via it's setter method.

Event Observer Methods

public class MyEventHandler {     public void processSomeEvent(@Observes final SomeEvent event) {     } } 

The event instance is injected into the event handling method directly (though, not with the @Inject annotation)

Producer Methods

public class ProcessorFactory {     @Produces public Processor getProcessor(@Inject final Gateway gateway) {         // ...     } } 

Parameters to producer methods automatically get injected.

like image 69
Perception Avatar answered Sep 23 '22 02:09

Perception


If what you REALLY want is not something as the parameter of the method (which should be provided by the caller), but a properly initialized instance of a CDI bean each time when the method is called, and fully constructed and injected, then check

javax.inject.Provider<T>

Basically, first inject a provider to the class

@Inject Provider<YourBean> yourBeanProvider; 

then, in the method, obtain a new instance

YourBean bean = yourBeanProvider.get(); 

Hope this helps :)

like image 26
Bing Ren Avatar answered Sep 23 '22 02:09

Bing Ren