<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

Spring-AOP标签scoped-proxy的更多相关文章

  1. Spring AOP 之编译期织入、装载期织入、运行时织入(转)

    https://blog.csdn.net/wenbingoon/article/details/22888619 一   前言 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP ...

  2. 代理模式及Spring AOP (二)

    一.Spring AOP   1.1 Spring AOP 底层还是用的动态代理.如果目标对象所对应的类有接口,spring就用jdk生成代理对象: 如果目标对象所对应的类没有接口,spring就用C ...

  3. Spring AOP 问与答

    AOP的实现有哪些 AOP常见的实现有: Spring AOP Aspectj Guice AOP Jboss AOP 等 AOP Alliance 是什么, 为什么Spring AOP, G UIC ...

  4. spring aop配置及用例说明(2)

    欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...

  5. spring Aop设计原理

    转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...

  6. Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy

    在使用Spring AOP时,遇到如下的错误: Exception in thread "main" java.lang.ClassCastException: com.sun.p ...

  7. Spring Aop(八)——advisor标签

    转发地址:https://www.iteye.com/blog/elim-2396274 8 advisor标签 advisor标签是需要定义在aspect标签里面的,其作用与aspect类似,可以简 ...

  8. 阿里四面:你知道Spring AOP创建Proxy的过程吗?

    Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...

  9. Spring AOP源码分析(二)动态A0P自定义标签

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 之前讲过Spring中的自定义注解,如果声明了自定义的注解,那么就一定 ...

  10. spring aop(四)

    直接找到解析aop标签的方法: protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate deleg ...

随机推荐

  1. 用AndroidStudio发布Libs到Bintray jCenter

    1 RootProject[根目录]build.gradle中添加如下插件引用 dependencies { ....... classpath 'com.jfrog.bintray.gradle:g ...

  2. python安装第三方库的三种方法

    使用pip 大多数库都可以通过pip安装,安装方法为,在命令行窗口输入 pip install libname libname为库名 某些库通过pip安装不了,可能是因为没有打包上传到pypi中,可以 ...

  3. ajax struts2 数据的返回形式

    这篇随笔算是接上篇的数据请求返回,上一篇关于分页用的返回的数据形式是json,http://www.cnblogs.com/tele-share/p/7192206.html这次主要探讨ajax从st ...

  4. oracle之 RA-00054: resource busy and acquire with NOWAIT specified or timeout expired

    1. truncate 表报 ORA-00054 ,标明有事务正在操作该表SQL> truncate table alldm.DM_XQKD_YUJING_D;truncate table al ...

  5. JSP中的“小饼干”Cookie,用来存储数组的方式(下方已String类型的数组为例:)

    1.Cookie常用方法中,存储数据的方式: Cookie cookie = new Cookie("key","Value"); response.addCo ...

  6. Shell脚本之反引号【``】和 $()

    一.奇怪的返回 今天在搞监控的时候,修改一个老脚本,主要是通过对操作系统进行判断来获取不同的监控参数.(获取top参数在不同操作系统上也有个坑,会在另外一篇里面写) 脚本如下,非常简单: #处理Cen ...

  7. JavaScript 数组对象的去重

    JavaScript数组去重 1.原型去重法.通过prototype找到数组的源性对象Array,在数组的原型上添加unique()方法.需要使用的时候使用 点 " . " 进行连 ...

  8. Matlab与C++混合编程(依赖OpenCV)

    Matlab与C++混合编程实际上就是通过Matlab的Mex工具将C++的代码编译成Matlab支持调用的可执行文件和函数接口.这样一方面可以在Matlab中利用已经编写好的函数,尽管这个函数是用C ...

  9. 使用socket搭建一个网络聊天室

    #服务器端import socket import threading #创建一个TCP端 sock = socket.socket(socket.AF_INET, socket.SOCK_STREA ...

  10. 前端 IoC 理念入门

    背景 近几年,前端应用(WebApp)正朝着大规模方向发展,在这个过程中我们会对项目拆解成多个模块/组件来组合使用,以此提高我们代码的复用性,最终提高研发效率. 在编写一个复杂组件的时候,总会依赖其他 ...