CDI compatible @ManagedProperty
In JSF 2.3, the built-in scope annotations are deprecated, there are some alternatives provided in CDI, eg.ApplicationScoped
, SessionScope
, RequestScoped
, etc.In JSF 2.2, JSF itself provided a CDI compatible
ViewScoped
, go here to view more details.There is an exception, we can not find an alternative for the legacy
@ManagedProperty
, there is a useful blog entry describe how to created a CDI compatible @ManagedProperty
from scratch.Fortunately, JSF 2.3 provides a built-in CDI implementation.
Let's create a sample to try it.
Create a simple backing bean.
import javax.faces.annotation.ManagedProperty;
//...other imports
@Model
public class BackingBean {
@Inject
@ManagedProperty("#{fooBean.bar}")
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@ManagedProperty
is from package javax.faces.annotation
, which is newly added in JSF 2.3.fooBean
is a CDI bean, which has a property named bar
.@Named
public class FooBean {
private String bar = "bar from FooBean";
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
BackingBean
.<div>
CDI managed property: #{backingBean.message}
</div>
Grab the source codes from my github account, and have a try.
评论