Spring-AOP標簽scoped-proxy


<aop:scoped-proxy/>介紹:

  Spring的Bean是有scope屬性的,表示bean的生存周期。scope的值有prototype、singleton、session、request。那么就有個問題了,如果一個singleton的bean中引用了一個prototype的bean,結果會怎樣呢?在默認情況下,單例會永遠持有一開始構造所賦給它的值。

所以,為了讓我們在每次調用這個Bean的時候都能夠得到具體scope中的值,比如prototype,那么我們希望每次在單例中調用這個Bean的時候,得到的都是一個新的prototype,Spring中AOP名字空間中引入了這個標簽。 <aop:scoped-proxy/>。下面具體看一個例子:

步驟一:創建兩個bean。一個將來的生存周期是singleton,一個將來的生存周期是prototype

package org.burning.sport.model.proxy;

import java.util.Date;

public class PrototypeBean {
    private Long timeMilis;

    public PrototypeBean() {
        this.timeMilis = new Date().getTime();
    }

    public void printTime() {
        System.out.println("timeMils:" + timeMilis);
    }
}
package org.burning.sport.model.proxy;

public class SingletonBean {
    private PrototypeBean prototypeBean; public void setPrototypeBean(PrototypeBean prototypeBean) {
        this.prototypeBean = prototypeBean;
    }

    public void printTime() {
        prototypeBean.printTime();
    }

}

 

步驟二:新建一個xml文件scopedProxyBean.xml。用來創建bean並且添加相互的依賴關系

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">
    <bean id="prototypeBean" class="org.burning.sport.model.proxy.PrototypeBean" scope="prototype">
        <aop:scoped-proxy/>
    </bean>
    <bean id="singletonBean" class="org.burning.sport.model.proxy.SingletonBean">
        <property name="prototypeBean">
            <ref bean="prototypeBean"/>
        </property>
    </bean>
</beans>

 

步驟三:寫一個單元測試,觀察效果

package bean;

import org.burning.sport.model.proxy.SingletonBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:scopedProxyBean.xml"})
public class ScopedProxyTest {
    @Autowired
    private SingletonBean singletonBean;
    @Test
    public void proxyTest() {
        singletonBean.printTime();
        System.out.println("===============");
        singletonBean.printTime();
    }
}

 

結果:

timeMils:1512617912901
===============

timeMils:1512617913009

總結:我們看到同一個singletonbean打印出來的時間是不一樣的,得知prototypeBean是維持了自己的"prototype"生存周期


 

步驟四:把scopedProxyBean.xml中的<aop:scoped-proxy/>注釋掉再運行單元測試看輸出結果

結果:

timeMils:1512618144214
===============
timeMils:1512618144214

結論:輸出的結果是一致的,得知prototypeBean的生存周期被改變為跟singletonbean一樣的“singleton”

參考:

[1]博客,http://blog.csdn.net/Mr_SeaTurtle_/article/details/52992207


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM