当前位置: 首页 > news >正文

企业网站建设的意义广州企业推广

企业网站建设的意义,广州企业推广,三亚新闻发布会直播第十五场,安徽网络推广排名Spring源码阅读目录 第一部分——IOC篇 第一章 Spring之最熟悉的陌生人——IOC 第二章 Spring之假如让你来写IOC容器——加载资源篇 第三章 Spring之假如让你来写IOC容器——解析配置文件篇 第四章 Spring之假如让你来写IOC容器——XML配置文件篇 第五章 Spring之假如让你来写…

Spring源码阅读目录

第一部分——IOC篇

第一章 Spring之最熟悉的陌生人——IOC
第二章 Spring之假如让你来写IOC容器——加载资源篇
第三章 Spring之假如让你来写IOC容器——解析配置文件篇
第四章 Spring之假如让你来写IOC容器——XML配置文件篇
第五章 Spring之假如让你来写IOC容器——BeanFactory和FactoryBean
第六章 Spring之假如让你来写IOC容器——Scope和属性填充
第七章 Spring之假如让你来写IOC容器——属性填充特别篇:SpEL表达式
第八章 Spring之假如让你来写IOC容器——拓展篇
第九章 Spring之源码阅读——环境搭建篇
第十章 Spring之源码阅读——IOC篇

第二部分——AOP篇

第十一章 Spring之不太熟的熟人——AOP
第十二章 Spring之不得不了解的内容——概念篇
第十三章 Spring之假如让你来写AOP——AOP联盟篇
第十四章 Spring之假如让你来写AOP——雏形篇
第十五章 Spring之假如让你来写AOP——Joinpoint(连接点)篇
第十六章 Spring之假如让你来写AOP——Pointcut(切点)篇
第十七章 Spring之假如让你来写AOP——Advice(通知)上篇
第十八章 Spring之假如让你来写AOP——Advice(通知)下篇
第十九章 Spring之假如让你来写AOP——番外篇:Spring早期设计
第二十章 Spring之假如让你来写AOP——Aspect(切面)篇
第二十一章 Spring之假如让你来写AOP——Weaver(织入器)篇
第二十二章 Spring之假如让你来写AOP——Target Object(目标对象)篇
第二十三章 Spring之假如让你来写AOP——融入IOC容器篇
第二十四章 Spring之源码阅读——AOP篇


文章目录

  • Spring源码阅读目录
    • 第一部分——IOC篇
    • 第二部分——AOP篇
  • 前言
  • 尝试动手写IOC容器
      • 第二十二版 Target Object(目标对象)
  • 总结


前言

    对于Spring一直都是既熟悉又陌生,说对它熟悉吧,平时用用没啥问题,但面试的时候被问的一脸懵逼,就很尴尬,都不好意思在简历上写着熟悉Spring了
在这里插入图片描述

    所以决定花点时间研究研究Spring的源码。主要参考的书籍是:《Spring源码深度解析(第2版)》、《Spring揭秘》、《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》


    书接上回,在上篇 第二十一章 Spring之假如让你来写AOP——Weaver(织入器)篇 中,A君 已经完成了 Weaver(织入器) 部分,整个 AOP 也进入了尾声啦。接下来看看 A君 会有什么骚操作吧

尝试动手写IOC容器

    出场人物:A君(苦逼的开发)、老大(项目经理)

    背景:老大要求A君在一周内开发个简单的 IOC容器

    前情提要: A君 已经完成了 Weaver(织入器) 部分,整个 AOP 也进入了尾声 。。。

第二十二版 Target Object(目标对象)

    “A君 呐,折腾了这么久,总算快完事了。AOP 还差最后一部分内容了。你对 Target Object(目标对象) 理解似乎不够,直接用 Object 来代表目标对象,你忘了件事,之前在做 IOC容器 的时候,可是存在 作用域(Scope) 的,也就是说 Target Object(目标对象) 可以是单例,也可以是多例。再往广的说:也有可能是对象池,亦或是 ThreadLocal,甚至于用户自定义,你直接用 Object 显然满足不了这么多的需求。拿回去改改吧!”

    “果然!老大 不是好糊弄的” 虽然 A君 早有心理准备,心里还是不免一阵感叹

    感叹归感叹,活还是少不了的。不过类似的话,A君 在写 IOC容器 时,都快听腻了,面向接口编程,懂!懂!懂!A君 提取一个 TargetSource 接口,作为所有 Target Object(目标对象) 的标准,代码如下:

/*** 切面目标对象接口*/
public interface TargetSource {/*** 获取目标对象class对象** @return*/Class<?> getTargetClass();/*** 是否是静态对象* 什么是静态对象?* 单例这种可以算静态** @return*/boolean isStatic();/*** 获取目标对象** @return* @throws Exception*/Object getTarget() throws Exception;/*** 释放对象* 用于对象池之类的实现时,才有用** @param target* @throws Exception*/void releaseTarget(Object target) throws Exception;
}

好了,接口有了。接下来就是考虑具体实现的问题了。对于单例对象没什么好说的,直接定义一个 final 修饰的属性即可,问题在于多例上面,先易后难吧。A君 定义个 SingletonTargetSource 类。代码如下:

import com.hqd.ch03.v22.aop.TargetSource;
import lombok.AllArgsConstructor;/*** 单例 targetSource*/
@AllArgsConstructor
public class SingletonTargetSource implements TargetSource {private final Object target;@Overridepublic Class<?> getTargetClass() {return this.target.getClass();}@Overridepublic boolean isStatic() {return true;}@Overridepublic Object getTarget() {return target;}@Overridepublic void releaseTarget(Object target) throws Exception {}/*** @param other* @return*/@Overridepublic boolean equals(Object other) {if (other instanceof SingletonTargetSource) {return ((SingletonTargetSource) other).target.equals(this);}return false;}@Overridepublic int hashCode() {return target.hashCode();}
}

单例弄完之后,现在轮到多例了,多例细说的话,也可以分为很多种:对象池、Scope、ThreadLocal等。经过前段时间的洗礼,A君 几乎形成条件反射了,一旦涉及到多个实现,首先想到的就是能否定义抽象类,提取公共代码。“要说到公共部分,那就是 Target Object(目标对象) 应该都是从 IOC容器 中获取。” A君 心想。于是,A君 先把这部分内容提取成一个抽象类,AbstractBeanFactoryBasedTargetSource 代码如下:

import com.hqd.ch03.v22.aop.TargetSource;
import com.hqd.ch03.v22.aware.BeanFactoryAware;
import com.hqd.ch03.v22.factory.BeanFactory;public abstract class AbstractBeanFactoryBasedTargetSource implements BeanFactoryAware, TargetSource {private final String targetBeanName;private BeanFactory beanFactory;private volatile Class<?> targetClass;public AbstractBeanFactoryBasedTargetSource(String targetBeanName) {this.targetBeanName = targetBeanName;}@Overridepublic Class<?> getTargetClass() {try {if (this.targetClass != null) {return this.targetClass;}synchronized (this) {this.targetClass = this.beanFactory.getType(targetBeanName);/*获取失败,这创建对象获取*/if (this.targetClass == null) {Object bean = this.beanFactory.getBean(targetBeanName);if (bean != null) {this.targetClass = bean.getClass();}}}} catch (Exception e) {throw new RuntimeException(e);}return this.targetClass;}@Overridepublic boolean isStatic() {return false;}public BeanFactory getBeanFactory() {return beanFactory;}@Overridepublic void setBeanFactory(BeanFactory beanFactory) {if (this.targetBeanName == null) {throw new IllegalStateException("'targetBeanName' 值为空");}this.beanFactory = beanFactory;}public String getTargetBeanName() {return targetBeanName;}@Overridepublic void releaseTarget(Object target) throws Exception {}//TODO 重新equals
}

AbstractBeanFactoryBasedTargetSource 只是提供了 BeanFactory,多例的话还需要创建对象,销毁等操作,A君 在提取一个抽象类——AbstractPrototypeBasedTargetSource,代码如下:


import com.hqd.ch03.v17.aop.target.AbstractBeanFactoryBasedTargetSource;
import com.hqd.ch03.v17.factory.BeanFactory;public class AbstractPrototypeBasedTargetSource extends AbstractBeanFactoryBasedTargetSource {public AbstractPrototypeBasedTargetSource(String targetBeanName) {super(targetBeanName);}@Overridepublic void setBeanFactory(BeanFactory beanFactory) {super.setBeanFactory(beanFactory);try {/*** 不是多例抛出异常*/if (!beanFactory.isPrototype(getTargetBeanName())) {throw new RuntimeException("bean不是多例");}} catch (Exception e) {throw new RuntimeException(e);}}/*** 创建对象** @return*/protected Object newPrototypeInstance() {return this.getBeanFactory().getBean(getTargetBeanName());}protected void destroyPrototypeInstance(Object target) {//TODO 销毁对象}
}

公共代码都提取完毕了,多例也就没有多少代码了。只需要调用父类实现即可,PrototypeTargetSource 代码如下:

public class PrototypeTargetSource extends AbstractPrototypeBasedTargetSource {public PrototypeTargetSource(String targetBeanName) {super(targetBeanName);}@Overridepublic Object getTarget() {return newPrototypeInstance();}@Overridepublic void releaseTarget(Object target) throws Exception {destroyPrototypeInstance(target);}
}

TargetSource 实现类都准备完毕了,还需要对 AdvisedSupport 进行一番改造,将Object替换成 TargetSource 即可,代码如下:

在这里插入图片描述

还有一个改造点在 CglibAopProxy,需要根据是否单例来创建不同的方法拦截器。如果非单例的话,每次都需要获取

在这里插入图片描述

DynamicUnadvisedExposedInterceptor 代码如下:

   private static class DynamicUnadvisedExposedInterceptor implements MethodInterceptor, Serializable {private final TargetSource targetSource;public DynamicUnadvisedExposedInterceptor(TargetSource targetSource) {this.targetSource = targetSource;}@Overridepublic Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {Object oldProxy = null;Object target = this.targetSource.getTarget();try {oldProxy = AopContext.setCurrentProxy(proxy);Object retVal = invokeMethod(target, method, args, methodProxy);return processReturnType(proxy, target, method, retVal);} finally {AopContext.setCurrentProxy(oldProxy);if (target != null) {this.targetSource.releaseTarget(target);}}}}

    接下来又到了愉快的测试环节,不过,在编写测试时,A君 发现了一个问题:按理说,Target Object(目标对象) 应该是根据Scope来创建不同的实现类,单例就创建 SingletonTargetSource,多例就创建 PrototypeTargetSource。后续和 IOC容器 整合,创建Bean时候应该返回代理对象,那么代理对象和目标对象的Scope是一样的, Target Object(目标对象) 处境似乎有点尴尬了,如果目标对象是单例,那么 Target Object(目标对象) 可以是 SingletonTargetSource,目标对象是多例, Target Object(目标对象) 也可以是 SingletonTargetSource,因为代理对象和目标对象的Scope是一致的,那么是 SingletonTargetSource 还是 PrototypeTargetSource 意义不大。如此说来,只有一种情况才能解释这个设计,那就是目标对象是多例,而代理对象是单例

    “算了,不想了,回头问下 老大。” A君 放弃了,继续开始它的测试之旅,其实也没得测试,现在还没有和 IOC 结合,只能看下是否是使用 TargetSource 即可,测试代码如下:

 @Testpublic void v17() throws Throwable {System.out.println("############# 第十七版  Target Object(目标对象) #############");AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut("execution(* *.test(..))");Object ap = new AopBean();Method method = AopBean.class.getDeclaredMethod("test");AopTest aop = new AopTest();Method beforeTest = aop.getClass().getDeclaredMethod("beforeTest");Method afterTest = aop.getClass().getDeclaredMethod("afterTest");Method afterReturnTest = aop.getClass().getDeclaredMethod("afterReturnTest");List<MethodInterceptor> list = new ArrayList<>();//前置通知AspectJMethodBeforeAdvice aspectJMethodBeforeAdvice = new AspectJMethodBeforeAdvice(pointcut, beforeTest, aop);MethodBeforeAdviceInterceptor methodBeforeAdviceInterceptor = new MethodBeforeAdviceInterceptor(aspectJMethodBeforeAdvice);list.add(methodBeforeAdviceInterceptor);//后置通知list.add(new AspectJAfterAdvice(pointcut, afterTest, aop));//返回通知AspectJAfterReturningAdvice aspectJAfterReturningAdvice = new AspectJAfterReturningAdvice(pointcut, afterReturnTest, aop);list.add(new AfterReturningAdviceInterceptor(aspectJAfterReturningAdvice));ProxyFactory proxyFactory = new ProxyFactory(AopBean.class, ap, method);proxyFactory.addAdvisors(list);AopBean proxy = (AopBean) proxyFactory.getProxy();proxy.test();System.out.println(proxyFactory.getTargetSource().getClass());}

测试结果如下:

在这里插入图片描述

“OK,今天的任务完成了” A君 欢呼

小插曲: 下班前,A君 终于逮到了 老大,把自己的疑问说了出来,老大 回答道:“其实 Spring 除了 ProxyFactory,还有 ProxyFactoryBean 不仅能把代理工厂融入到 IOC,还能把代理的细节都交给开发,包括你提到的 TargetObject,只是 Spring 默认使用 ProxyFactory,屏蔽了这些东西,就是 ProxyFactory 也有 TargetSourceCreator 可以配置自定义创建 TargetSource


总结

    正所谓树欲静而风不止,欲知后事如何,请看下回分解(✪ω✪)


文章转载自:
http://opium.xqwq.cn
http://kcal.xqwq.cn
http://roundheel.xqwq.cn
http://safety.xqwq.cn
http://ila.xqwq.cn
http://tinkerly.xqwq.cn
http://choana.xqwq.cn
http://had.xqwq.cn
http://campylotropous.xqwq.cn
http://exclusionism.xqwq.cn
http://underdose.xqwq.cn
http://kilometrage.xqwq.cn
http://asclepiad.xqwq.cn
http://paracyesis.xqwq.cn
http://smithery.xqwq.cn
http://libau.xqwq.cn
http://usurer.xqwq.cn
http://computerese.xqwq.cn
http://bagworm.xqwq.cn
http://hypotensive.xqwq.cn
http://wuhan.xqwq.cn
http://baresark.xqwq.cn
http://instantial.xqwq.cn
http://langrage.xqwq.cn
http://dimethylbenzene.xqwq.cn
http://jumar.xqwq.cn
http://ultrashort.xqwq.cn
http://cassocked.xqwq.cn
http://oilseed.xqwq.cn
http://rap.xqwq.cn
http://junkie.xqwq.cn
http://nosophobia.xqwq.cn
http://everwhich.xqwq.cn
http://nonreliance.xqwq.cn
http://shocking.xqwq.cn
http://awn.xqwq.cn
http://effacement.xqwq.cn
http://dyeline.xqwq.cn
http://airframe.xqwq.cn
http://dccc.xqwq.cn
http://millions.xqwq.cn
http://amg.xqwq.cn
http://document.xqwq.cn
http://otherwise.xqwq.cn
http://linguaphone.xqwq.cn
http://photodramatist.xqwq.cn
http://encage.xqwq.cn
http://semplice.xqwq.cn
http://pithead.xqwq.cn
http://potass.xqwq.cn
http://australioid.xqwq.cn
http://inequilaterally.xqwq.cn
http://weatherproof.xqwq.cn
http://phosphorylation.xqwq.cn
http://sawtimber.xqwq.cn
http://landfall.xqwq.cn
http://naturalize.xqwq.cn
http://chalybeate.xqwq.cn
http://inwit.xqwq.cn
http://inaudibility.xqwq.cn
http://racquet.xqwq.cn
http://xanthomatosis.xqwq.cn
http://indexical.xqwq.cn
http://spinode.xqwq.cn
http://diestrum.xqwq.cn
http://winebottle.xqwq.cn
http://shuffleboard.xqwq.cn
http://felting.xqwq.cn
http://unreservedly.xqwq.cn
http://setteron.xqwq.cn
http://trepid.xqwq.cn
http://camail.xqwq.cn
http://clamorously.xqwq.cn
http://policemen.xqwq.cn
http://movability.xqwq.cn
http://ammoniate.xqwq.cn
http://unpiloted.xqwq.cn
http://haematometer.xqwq.cn
http://accoutrements.xqwq.cn
http://prefocus.xqwq.cn
http://lurch.xqwq.cn
http://patternize.xqwq.cn
http://lassen.xqwq.cn
http://fallage.xqwq.cn
http://aero.xqwq.cn
http://cerebroid.xqwq.cn
http://anamorphoscope.xqwq.cn
http://soleiform.xqwq.cn
http://moodily.xqwq.cn
http://whirry.xqwq.cn
http://cadastre.xqwq.cn
http://wetly.xqwq.cn
http://replenishment.xqwq.cn
http://procession.xqwq.cn
http://rapturous.xqwq.cn
http://millennialist.xqwq.cn
http://cranage.xqwq.cn
http://gogo.xqwq.cn
http://benzedrine.xqwq.cn
http://mahaleb.xqwq.cn
http://www.hrbkazy.com/news/84005.html

相关文章:

  • 做网站 需要了解什么网站打开速度优化
  • 做a视频 免费网站怎么进行网络推广
  • 做网站用模版引流黑科技app
  • 微信公众平台可以导入wordpressseo查询工具
  • 做网站建设需要什么资质爱站工具
  • 湖北做网站的seo公司服务
  • javaweb做视频网站原理网站营销方案
  • 电视台网站建设方案.doc国内专业seo公司
  • 唐山网站建设模板广州seo推广优化
  • 厦门哪些做鲜花的网站个人免费建站软件
  • 网站开发工程师发展趋势运营培训班学费大概多少
  • .net如何做网站安卓优化大师
  • 专门做母婴的网站有哪些宣传平台有哪些
  • 购物网站建设需要什么资质地推平台去哪里找
  • 网站右下角弹窗代码怎么营销推广
  • 国外建站工具一个新手如何推销产品
  • wordpress最近评论seo推广费用需要多少
  • 北京网站建设网络公司北京营销推广网站建设
  • 技能培训中心网站建设外贸推广具体是做什么
  • 中小型企业网站建设网站优化的方式有哪些
  • 做简易网站聊城优化seo
  • 张家港市人民政府关于网站建设什么推广软件效果好
  • 国外做logo的网站青岛seo关键词优化排名
  • 简单网站开发实例汇总补习班
  • 一个网站的建设要经过哪几个阶段百度浏览器在线打开
  • 邢台做外贸网站高级seo课程
  • 网站制作公司大型外链代发免费
  • 有网站怎么做seo推广营销活动有哪些
  • 提升政府网站内容建设网站里的友情链接
  • 网站开发费用报价表百度编写网站