Quantcast
Channel: OmniFaces & JSF Fans
Viewing all articles
Browse latest Browse all 74

CDI-JSF: Use @Inject to annotate a field of a bean

$
0
0
Most commonly, JSF applications uses dependency injection by annotating private fields with @Inject. We can inject directly an instance of another class (bean) or we can inject an javax.enterprise.inject.Instance and use the get() method to obtain the needed instance:

@Named
@RequestScoped
public class TheBean {

 private static final Logger LOG = Logger.getLogger(TheBean.class.getName());

 @Inject
 private Foo foo;
 //private Instance<Foo> foo;
   
 @Inject
 private Buzz buzz;
 //private Instance<Buzz> buzz;
   
 @Inject
 private Bizz bizz;   
 //private Instance<Bizz> bizz;

 public void callFooBuzzBizz() {
  LOG.info("TheBean#callFooBuzzBizz() called ...");
  Messages.addGlobalInfo("TheBean#callFooBuzzBizz() called ...");
       
  foo.fooify();
  buzz.buzzify();
  bizz.bizzify();
       
  // foo.get().fooify();
  // buzz.get().buzzify();
  // bizz.get().bizzify();
 }
}

The complete example is available here.

Viewing all articles
Browse latest Browse all 74

Trending Articles