深入理解Spring AOP之二代理对象生成
深入理解Spring AOP之二代理对象生成
spring代理对象
- 假设目标对象的实现类实现了接口。Spring AOP 将会採用 JDK 动态代理来生成 AOP 代理类;
- 假设目标对象的实现类没有实现接口,Spring AOP 将会採用 CGLIB 来生成 AOP 代理类
Spring AOP 使用类 org.springframework.aop.framework.ProxyFactory进行织入。找到ProxyFactory相应的相关内容,然后整理例如以下类图
Spring代理类怎样生成
先看ProxyFactory是怎样得到代理类的
public Object getProxy() {
return createAopProxy().getProxy();
}
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this);
}
ProxyFactoryBean是怎样获得代理类的
所以。假设容器中某个对象依赖于ProxyFactoryBean,那么它将会使用到ProxyFactoryBean的getObject()方法所返回的代理对象
@Override
public Object getObject() throws BeansException {
initializeAdvisorChain(); //初始化通知器
if (isSingleton()) {
return getSingletonInstance();//依据定义生成单例的Proxy
}
else {
if (this.targetName == null) {
logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " +
"Enable prototype proxies by setting the 'targetName' property.");
}
return newPrototypeInstance(); //这里依据定义生成prototype的Proxy
}
}
private synchronized Object getSingletonInstance() {
if (this.singletonInstance == null) {
this.targetSource = freshTargetSource();//返回被 代 理的 目标对象
if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
//从targetSource中获取目标对象的Class
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
}
//这里设置代理对象的接口 setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
}
// 初始化共享的单例
super.setFrozen(this.freezeProxy);
//这里会使用ProxyFactory来生成须要的Proxy
this.singletonInstance = getProxy(createAopProxy());
}
return this.singletonInstance;
}
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this); //这里借助了AopProxyFactory
}
public AopProxyFactory getAopProxyFactory() {
return this.aopProxyFactory;
}
private AopProxyFactory aopProxyFactory;
public ProxyCreatorSupport() {
this.aopProxyFactory = new DefaultAopProxyFactory();
}
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?
> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
@Override
public Object getProxy(ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
}
//依据advised 中 的 配 置信息,将proxy须要代 理的接口放入proxiedInterfaces 中
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
//以下这种方法眼熟吧,哈哈 没错就是JDK中的动态代理经典方法
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
总结
第一种获取比較简单。可是须要手工的进行写代码,而另外一种是通过Spring的IOC机制来控制Bean的生成。
深入理解Spring AOP之二代理对象生成的更多相关文章
- 死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Spring Aop(十四)——Aop自动创建代理对象的原理
转发地址:https://www.iteye.com/blog/elim-2398725 Aop自动创建代理对象的原理 我们在使用Spring Aop时,通常Spring会自动为我们创建目标bean的 ...
- Spring AOP 和 动态代理技术
AOP 是什么东西 首先来说 AOP 并不是 Spring 框架的核心技术之一,AOP 全称 Aspect Orient Programming,即面向切面的编程.其要解决的问题就是在不改变源代码的情 ...
- AOP代理对象生成
AOP(Aspect-OrientedProgramming,面向方面编程)是OOP(Object-Oriented Programing,面向对象编程)的良好补充与完善,后者侧重于解决 从上到下的存 ...
- spring AOP 之二:@AspectJ注解的3种配置
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- Spring AOP之动态代理
软件151 李飞瑶 一.Spring 动态代理中的基本概念 1.关注点(concern) 一个关注点可以是一个特定的问题,概念.或者应用程序的兴趣点.总而言之,应用程序必须达到一个目标 ...
- spring 理解Spring AOP 一个简单的约定游戏
应该说AOP原理是Spring技术中最难理解的一个部分,而这个约定游戏也许会给你很多的帮助,通过这个约定游戏,就可以理解Spring AOP的含义和实现方法,也能帮助读者更好地运用Spring AOP ...
- 深入理解Spring AOP 1.0
本文相关代码(来自官方源码spring-test模块)请参见spring-demysify org.springframework.mylearntest包下. 统称能够实现AOP的语言为AOL,即( ...
- 深入理解Spring AOP思想
什么是AOP?AOP解决了什么问题? 在传统的开发模式中,以下层次的是非常常见的一种,业务层每一个方法都要有重复的事务代码 如何改善这个问题? AOP希望将A.B 这些分散在各个业务逻辑中的相同代码, ...
随机推荐
- idea 下 encodings.xml 的正确位置
在多个module存在的情况下 encodings.xml在 project 下的.idea 下面 这个就是最父级project
- [转]SOA架构设计经验分享—架构、职责、数据一致性
阅读目录: 1.背景介绍 2.SOA的架构层次 2.1.应用服务(原子服务) 2.2.组合服务 2.3.业务服务(编排服务) 3.SOA化的重构 3.1.保留服务空间,为了将来服务的组合 4.运用DD ...
- mac os系统go安装:go install github.com/nsf/gocode: open /usr/local/go/bin/gocode: permission denied
gocode是go语言代码自动提示工具 安装时进入src目录执行:go get -u github.com/nsf/gocode 出现: github.com/nsf/gocode (download ...
- 【转】WPF自定义控件与样式(10)-进度控件ProcessBar自定义样
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: ProcessBar自定义标准样式: ProcessBar自定义环形进 ...
- 启动matlab时总是直接跳转到安装界面的解决方案
[关于2017双11过后MATLAB许可过期问题的解决方案] 在距离双11还有2个月之前,matlab会提示:Your MATLAB license will expire in 50 days -- ...
- What is a TensorFlow Session?
Sep 26, 2016 I've seen a lot of confusion over the rules of tf.Graph and tf.Session in TensorFlow. I ...
- Manual Validation with Data Annotations C#对实体类进行手动验证
Several people have asked me about using data annotations for validation outside of a UI framework, ...
- JS -- serializeJSON
http://www.cnblogs.com/linzenews/p/7065050.html
- FastJSON 设置默认参数,全局配置方式 和 一些使用方式、坑
如果要被序列化的对象含有一个date属性或者多个date属性按照相同的格式序列化日期的话,那我们可以使用下面的语句实现: 在应用的的Main方法体里配置全局参数: JSONObject.DEFFAUL ...
- JIRA应用的内存参数设置不当+容器没有对资源进行限制导致服务挂掉的例子
背景: 应用的部署结构是这样的:使用rancher管理的Docker集群,有三台物理主机,二十多个Docker容器, 提供的功能是问题跟踪(JIRA),文档管理(Confluence),代码托管(sv ...