My name is Vasyl Khrystiuk‎ > ‎process‎ > ‎main‎ > ‎

own spring bean scope

Ok. there shuld be some reasones for implementing own bean scope.
In my case i have web application, that load some data from database, whan user is logined, and user work with that data im memory - fast read and asyng write back to db. And when user is login out - i clear working memory(checking if all async writes is completed).Also my application support multilogin for one user. So it is logicaly, that if user have two active sessions - working memory shoult be same. And if user is logout from one session, but another is still live - working memory shoult keep for serving secont session.  So I desice to create own scope - "useronline". There will be code example of such implementation using standart j2ee api and spring framework.

There is another use-cases of scope usage:
Something interesting about beans proxying and costum scope: http://stackoverflow.com/a/6152767/449553



note: 
myScope.clearBean();
is called from external code. This is good example - it show who is responsible for cleaning scope, when it is out of date. There are another examples based on thread scope an that are store data in thread local - but that example is worse as far as threadlocal no need to clean - it is cleaned automaticaly when thread is dying. 

There is another good example with description.

And more:





And as a note - this is great example, and I probably will use this code in my future apps, but in current one i did not use it, becauce current one based on plain j2ee api(in order to that fact, that this project main part is websockets, with is not supported by spring fully). So i have own realization of this idea based on own web app listener, wich handle memory loading/unloading on user session create/delete. Anyway, writting own scope handler is interesting task, so i did the work.

Еще вариант использования:
Скоуп по ип-аддресу. Т.е если надо собирать данные об пользователю на основании ип аддреса, но не создавать/зависеть от сессии. Конечно, мы не можем хранить все ип аддреса постоянно в памяти, потому скоупи должен иметь конец - например 15 минут после последного использования ип аддреса. И лучше всего это 15 минут отмерять с помощью Executors.newScheduledThreadPool




Comments