marți, 31 mai 2016

CDI - JSF: Bean constructor annotated with @Inject example

Generally speaking, any Java class that has a constructor without parameters or a constructor annotated with @Inject is a bean. This means that we can write a bean like this:

@Named
@RequestScoped
public class TheBean {

 private Foo foo;
 private Buzz buzz;
 private Bizz bizz;

 @Inject
 public TheBean(Foo foo, Buzz buzz, Bizz bizz) {
  this.foo = foo;
  this.buzz = buzz;
  this.bizz = bizz;
 }

 public void callFooBuzzBizz() {
  // do something with foo, buzz and bizz
 }
}

A complete example is available here.

Niciun comentariu:

Trimiteți un comentariu