Dears,
I try to use View Scope in Spring and I get the following steps as straightforward:
1- create the View Scope as following:
package scope;
import java.util.Map;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
public class ViewScope implements Scope {
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
if(viewMap.containsKey(name)) {
return viewMap.get(name);
} else {
Object object = objectFactory.getObject();
viewMap.put(name, object);
return object;
}
}
@Override
public Object remove(String name) {
return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
}
@Override
public String getConversationId() {
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
//Not supported
}
@Override
public Object resolveContextualObject(String key) {
return null;
}
}
2- in the configuration class, register the bean as the following code:
@Configuration
..............
public class Application implements Serializable{
...............
...............
@Bean
public CustomScopeConfigurer viewScope () {
CustomScopeConfigurer configurer = new CustomScopeConfigurer ();
configurer.addScope("view", new ViewScope());
return configurer;
}
3- In the Spring component class, use the Annotation Scope with name “view”:
@scope(“view”)
Thanks & Regards