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
Spring-AOP标签scoped-proxy的更多相关文章
- Spring AOP 之编译期织入、装载期织入、运行时织入(转)
https://blog.csdn.net/wenbingoon/article/details/22888619 一 前言 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP ...
- 代理模式及Spring AOP (二)
一.Spring AOP 1.1 Spring AOP 底层还是用的动态代理.如果目标对象所对应的类有接口,spring就用jdk生成代理对象: 如果目标对象所对应的类没有接口,spring就用C ...
- Spring AOP 问与答
AOP的实现有哪些 AOP常见的实现有: Spring AOP Aspectj Guice AOP Jboss AOP 等 AOP Alliance 是什么, 为什么Spring AOP, G UIC ...
- spring aop配置及用例说明(2)
欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...
- spring Aop设计原理
转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...
- Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy
在使用Spring AOP时,遇到如下的错误: Exception in thread "main" java.lang.ClassCastException: com.sun.p ...
- Spring Aop(八)——advisor标签
转发地址:https://www.iteye.com/blog/elim-2396274 8 advisor标签 advisor标签是需要定义在aspect标签里面的,其作用与aspect类似,可以简 ...
- 阿里四面:你知道Spring AOP创建Proxy的过程吗?
Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
- Spring AOP源码分析(二)动态A0P自定义标签
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 之前讲过Spring中的自定义注解,如果声明了自定义的注解,那么就一定 ...
- spring aop(四)
直接找到解析aop标签的方法: protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate deleg ...
随机推荐
- angular4.0配置本机IP访问项目
一.查看本机IP 命令行输入 ipconfig,在面板中选择IPv4地址 二.在项目中配置IP 打开package.json文件,修改如下 三.命令行运行 npm start 四.在手机上访问 192 ...
- ES6 Generators并发
ES6 Generators系列: ES6 Generators基本概念 深入研究ES6 Generators ES6 Generators的异步应用 ES6 Generators并发 如果你已经读过 ...
- python3之面向对象
1.面向对象术语 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类属性(类变量):类属性在整个实例化的对象中是公用的.类属 ...
- git命令记录
1, clone 远程分支 git clone 命令默认的只会建立master分支,如果你想clone指定的某一远程分支(如:dev)的话,可以如下: A. 查看所有分支(包括隐藏的) git br ...
- HTML知识点总结之table
table元素 table用来创建表格,表格也可以用来布局,但是嵌套过于复杂,不利于灵活布局,已经几乎没人用它来布局了. 表格基本上有如下几个标签构成: (1)<table>标签用来创建表 ...
- Redis随笔(二)redis desktop manager 安装并且连接redis服务器
1.首先在win10下安装redis desktop manager 2.查看虚拟机防火墙状态,启动状态,则关闭掉 查看防火墙状态: systemctl status firewalld.servic ...
- PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)
1.flush - 刷新输出缓冲 2.ob_clean - 清空(擦掉)输出缓冲区 此函数用来丢弃输出缓冲区中的内容. 此函数不会销毁输出缓冲区,而像 ob_end_clean() 函数会销毁输出缓冲 ...
- async/await 执行顺序详解
随着async/await正式纳入ES7标准,越来越多的人开始研究据说是异步编程终级解决方案的 async/await.但是很多人对这个方法中内部怎么执行的还不是很了解,本文是我看了一遍技术博客理解 ...
- c#的托管代码和非托管代码的理解
理解托管和非托管代码的前提之下,要先了解CLR(公共语言运行库) .Net Framework 是由彼此独立又相关的两部分组成:CLR 和 类库, CLR是它为我们提供的服务,类库是它实现的功能. . ...
- Activiti 5.22.0 之自由驳回任务实现(亲测)
上篇博文,我们完成一个任务SKIP的实现,说好要给各位看官带来驳回实现的现在,就奉上具体实现和讲解.(其实我感觉我的注释写的已经非常清楚了,哈哈) 依旧是,先说我们的需求和思路. PS: ...