zoukankan      html  css  js  c++  java
  • Spring——<aop:scoped-proxy/>理解

    首先看一下Spring文档上的两个例子对比:

    <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
    <bean id="userManager" class="com.foo.UserManager"> <property name="userPreferences" ref="userPreferences"/> </bean>

    另外一个例子:

    <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
        <aop:scoped-proxy/>
    </bean>
    
    <bean id="userManager" class="com.foo.UserManager">
        <property name="userPreferences" ref="userPreferences"/>
    </bean>
    

    如果只考虑容器对bean的实例化,scoped-proxy确实没什么意义,scoped-proxy的意义在关联bean之间的依赖时才能体现。

    前一个例子没有使用<aop:scoped-proxy/>,那么在这里userManager的作用域是singleton,容器中仅初始化一次,其作为属性userPreferences也仅被注入一次。当session失效后,容器中userManager的实例仍然存在,其属性userPreferences也随着存在,换句话说,userManager以后使用的userPreferences永远都是同一个。

    但后一个例子则不一样,userManager的属性userPreferences指向的是com.foo.UserPreferences实例的代理,当session过期后,userManager的属性userPreferences自然也不能再使用,也就是userManager的作为属性时的生命周期是按照自身声明的。

    那么proxy,它代理的工作就是——暴露这个bean令其符合其自身作用域。

  • 相关阅读:
    git分支
    git使用
    多人协作
    python初心记录二
    python初心记录一
    Javascript 概念类知识
    想成为前端工程师?希望读完这篇文章能对你有所帮助。
    Egret note
    cocos2d-js 连连看
    PS置入图片之后保留选区方便C图
  • 原文地址:https://www.cnblogs.com/qbzf-Blog/p/6538564.html
Copyright © 2011-2022 走看看