Spring-AOP标签scoped-proxy
2024-10-18 19:31:43
<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
最新文章
- block fomating context
- 后缀.jar的是什么文件?
- 【leetcode】Restore IP Addresses
- three.js光源
- [daily][device] linux添加打印机
- 修改datagridview中其中一列的值
- 转 用 AXIOM 促进 XML 处理
- Python多线程和Python的锁
- Unix/Linux环境C编程入门教程(12) openSUSECCPP以及Linux内核驱动开发环境搭建
- java爬虫查找四川大学所有学院的网站的网址中的通知和新闻——以计算机学院为例
- chrome开发工具指南(十四)
- JSON.parse()和eval()的区别
- 允许外网访问MySQL
- 【bzoj 3786】星系探索
- linux无法启动httpd服务问题
- [原]编译flightGear
- 使用C3P0报错:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
- Rpgmakermv(12) gacha插件系列
- mysql每天凌晨0点准时启动taskeng.exe如何关闭
- vertx连接mysql数据库
热门文章
- php 运算符and or &;&; || 的详解
- C盘里的桌面文件移到E盘里了,然后E盘里的文件都显示到桌面上了,怎么将桌面文件还原回C盘
- 小程序之ScrollView细节坑
- OLEDB数据源
- chrome调试工具高级不完整使用指南(实战二)
- MongoDb 快速入门教程
- 支付宝pc网页支付
- JSP最常用的五种内置对象(out,request,response,session,application)
- Codeforces Round #343 (Div. 2)-629A. Far Relative’s Birthday Cake 629B. Far Relative’s Problem
- BZOJ1786: [Ahoi2008]Pair 配对/1831: [AHOI2008]逆序对