I have a variable inside an application scoped bean. A user can trigger an update of this variable through a method call. Now the problem is that the user doesn't get an updated view of this variable after refreshing the jsf page. If have tested if the variable is updated properly and it is, so the method for updating is working correctly. Are variables inside an application scoped bean declared as final or what is the problem here?
Asked
Active
Viewed 818 times
1
-
Apparently it's not the same instance. You'd need to run a debugger or at least show an SSCCE so that we can see and point out your mistake. – BalusC Oct 25 '12 at 13:23
-
yes you were right. I thought an application scoped bean is only instantiated once. So if this isn't the case the only way to have only one instance is to pass it around within method signatures? In this case I think I'd prefer a Singleton Bean – nico1510 Oct 25 '12 at 15:57
-
Apparently you used the wrong annotations. I posted an answer. – BalusC Oct 25 '12 at 15:59
1 Answers
0
That can happen if you used the wrong combination of annotations. E.g.
import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;
@ManagedBean
@ApplicationScoped
public class App {}
Here, the scope annotation is from CDI and the bean management annotation is from JSF. JSF doesn't recognize CDI scope annotations and hence defaults to @NoneScoped
. I.e. the bean is reconstructed on every single EL #{app}
evaluation. This explains the symptoms you're seeing.
You'd need to fix the scope annotation to be from JSF as well.
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
@ManagedBean
@ApplicationScoped
public class App {}
The CDI scope annotations can only be used in combination with the CDI bean management annotation @Named
.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
-
Hmm, no I'm using the correct imports (both from javax.faces) and I inject the application scoped bean with @Inject inside an EJB... – nico1510 Oct 25 '12 at 16:17
-
It's really strange.. I did some debugging : One instance of the bean is created upon first request of the jsf page. The second instance is created upon creation of the EJB which uses the application scoped bean. After this whenever a new EJB instance is created the same (second) instance of the application scoped bean is used. But the user always only sees the application scoped bean which was created first and hence no view updates... – nico1510 Oct 25 '12 at 16:38
-
You can't inject JSF managed beans using `@Inject`. Replace it by CDI managed bean. – BalusC Oct 25 '12 at 16:45
-
I didn't quite get it. What's the correct way to instantiate the bean? – nico1510 Oct 25 '12 at 17:04
-
Is it even possible to inject an application scoped jsf managed bean into an EJB ? I can't find an example anywhere in tutorials/web serch... – nico1510 Oct 25 '12 at 17:23
-
It's usually to be done the other way round. Inject EJB as `@EJB` in the application scoped managed bean. If this is undesireable, then replace by CDI `@Named @ApplicationScoped`. – BalusC Oct 25 '12 at 18:04