user962466
user962466

Reputation: 11

Java Inject Applicationscope Bean

I try to inject a applicationScoped Bean. I found similar topics at JSF2 ApplicationScope bean instantiation time? and Get JSF managed bean by name in any Servlet related class

With the jsf way in faces context all is fine (WebsitesController is AppScoped):

FacesContext context = FacesContext.getCurrentInstance();
WebsiteController websitesController = context.getApplication().evaluateExpressionGet(context, "#{websitesController}", WebsitesController.class);

With the injections of the two overflow threads it doesn't work. My Code:

@ManagedBean(eager=true)
@ApplicationScoped
public class WebsitesController implements Serializable {
...}

and now i tried

@ManagedBean(name = "shopController")
@ViewScoped
public class ShopController {
    {Injection-Statement}
    private WebsitesController websitesController;

I tried following statements:

@ManagedProperty("#{websitesController}")
@Inject
@EJB

Whats my fault?

Upvotes: 0

Views: 1136

Answers (1)

iggi
iggi

Reputation: 11

I have also a problem with ApplicationScope on Glassfish. Do you have an maven or ant webproject? (With maven i think the ApplicationScope doesn´t work as expected - with ant it does)

Now to your question:

When you use @Inject, then your WebsiteController need @Named and @ApplicationScope (other imports as jsf !!) and you must have an beans.xml - then CDI is activated.

When you use @ EJB, then yout WebsiteController need @Stateless .

I hope i can help you...

Upvotes: 1

Related Questions