jsf2 spring managed bean calling constructor and postconstruct only on tomcatstartup
-
11-12-2019 - |
Question
I am using JSF+Spring project my spring maanged bean is ViewScoped
my saving state is client.
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
Following is my bean i found that constructor of bean and postconstruct method got call only on tomcat startup when it is spring managed bean instead if it is JSF managed bean constructor and postconstruct got call on every page refresh. Is it the real behavior if i want to call method on page refresh every time under spring managed bean what should id do ?
@Component
@ViewScoped
public class DataTableBean implements Serializable{
public DataTableBean() {
super();
}
@PostConstruct
private void loadDataData(){
System.out.println("Post constructing");
}
}
Solution
The @Component
is a Spring specific annotation to manage beans by Spring. The @ViewScoped
is a JSF specific annotation to specify the scope of a JSF managed bean @ManagedBean
. JSF specific scope annotations does not work on a Spring managed bean at all. You should use the Spring specific @Scope
annotation instead.
So, either just manage the bean by JSF:
@ManagedBean
@ViewScoped
Or homegrow a Spring "view" scope (this is namely not one of the default scopes available in Spring):
@Component
@Scope("view")