# 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](https://github.com/hantsy/ee7-sandbox/wiki/jsf-cdi) to view more details.

There is an exception, we can not find an alternative for the legacy `@ManagedProperty`, there is a [useful blog entry](http://www.manorrock.com/blog/2013/11/01/jsf_tip_31_migrate_your_managedproperty_annotations.html) 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.

```java
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;
    }

}
```

**NOTE**, `@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`.

```java
@Named
public class FooBean {

    private String bar = "bar from FooBean";

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }
}
```

In the facelets template, display the message of `BackingBean`.

```markup
<div>
    CDI managed property: #{backingBean.message}    
</div>
```

Run this application on Glassfish v5, open your browser and navigate to <http://localhost:8080/jsf-managedproperty/managedproperty.faces>.

![jsf cdi managedproperty](/files/-MVBIEpuTczA1zyxiibQ)

Grab the [source codes](https://github.com/hantsy/ee8-sandbox) from my GitHub account, and have a try.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hantsy.gitbook.io/java-ee-8-by-example/jsf/jsf-managedproperty.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
