样例1:
<
bean id
=
"
userPreferences
"
class
=
"
com.foo.UserPreferences
"
scope
=
"
session
"
/>
<
bean id
=
"
userManager
"
class
=
"
com.foo.UserManager
"
>
<
property name
=
"
userPreferences
"
ref
=
"
userPreferences
"
/>
</
bean
>
第一个例子: 如果session失效,userManager仍将保留userPreferences实例!
userManager直接将userPreference的引用拷贝了一份到自己的特定地址上!销毁了原来的实例,而拷贝后的实例依赖存在!
样例2:
<
bean id
=
"
userPreferences
"
class
=
"
com.foo.UserPreferences
"
scope
=
"
session
"
>
<
aop:scoped
-
proxy
/>
</
bean
>
<
bean id
=
"
userService
"
class
=
"
com.foo.SimpleUserService
"
>
<
property name
=
"
userPreferences
"
ref
=
"
userPreferences
"
/>
</
bean
>
加了<aop:scoped-proxy />,这个的作用是代理这个实例
第二个例子: session失效,则userPreferences失效. 单例的userManager保存的是userPreference的代理实例!
这个代理实例不是singleton的,也不是跟随着userPreferences的.它是随着session创建而创建... 如果session销毁,代理实例就无法访问到userPreference了!
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
第一个例子: 如果session失效,userManager仍将保留userPreferences实例!
userManager直接将userPreference的引用拷贝了一份到自己的特定地址上!销毁了原来的实例,而拷贝后的实例依赖存在!
样例2:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
加了<aop:scoped-proxy />,这个的作用是代理这个实例
第二个例子: session失效,则userPreferences失效. 单例的userManager保存的是userPreference的代理实例!
这个代理实例不是singleton的,也不是跟随着userPreferences的.它是随着session创建而创建... 如果session销毁,代理实例就无法访问到userPreference了!