View scope in Spring

If you want to have View Scope in Spring for JSF, firstly read here and here.

The problem with above was, those needed our @Scope(“view”) beans to be serialized.

We didn’t want to serialize our beans so we did the following workaround:

we defined our Autowired DAO classes as :

@Autowired
private transient DAO myDao;

and based on Rick’s above ViewScope implementation we added those 2 lines of code:

public class ViewScope implements Scope, ApplicationContextAware

….

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}


and finally in get method, after getting the object we did

applicationContext.getAutowireCapableBeanFactory().autowireBean(objToReturn);

to autowire the dependencies again.

hope it helps for you also…

Advertisements

One thought on “View scope in Spring

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s