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

企业网站建设的意义seo代码优化有哪些方法

企业网站建设的意义,seo代码优化有哪些方法,重庆做营销型网站建设公司,怎么样做网站卖农产品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://dinncoblastocele.stkw.cn
http://dinncoassai.stkw.cn
http://dinncoimprovisatorial.stkw.cn
http://dinncouptilt.stkw.cn
http://dinncobepuzzle.stkw.cn
http://dinncowoodprint.stkw.cn
http://dinncoseaboard.stkw.cn
http://dinncophonomotor.stkw.cn
http://dinncodogwatch.stkw.cn
http://dinncodefiniendum.stkw.cn
http://dinncotearjerker.stkw.cn
http://dinncodionysian.stkw.cn
http://dinncoselah.stkw.cn
http://dinncodofunny.stkw.cn
http://dinncoclipped.stkw.cn
http://dinncosalah.stkw.cn
http://dinncosemidaily.stkw.cn
http://dinncochuffy.stkw.cn
http://dinncopesade.stkw.cn
http://dinncojehu.stkw.cn
http://dinncomassachusetts.stkw.cn
http://dinncoskulker.stkw.cn
http://dinncohaphazardry.stkw.cn
http://dinncoentranceway.stkw.cn
http://dinncophotocomposition.stkw.cn
http://dinncoactivize.stkw.cn
http://dinncotinty.stkw.cn
http://dinncohemicellulose.stkw.cn
http://dinncounmindful.stkw.cn
http://dinncosaturant.stkw.cn
http://dinncobangzone.stkw.cn
http://dinncodeclassify.stkw.cn
http://dinncohalfhourly.stkw.cn
http://dinncodisagree.stkw.cn
http://dinncodrab.stkw.cn
http://dinncoparticular.stkw.cn
http://dinncomuttony.stkw.cn
http://dinncomassotherapy.stkw.cn
http://dinncopigeon.stkw.cn
http://dinncoadulterine.stkw.cn
http://dinncorepulse.stkw.cn
http://dinncobridesman.stkw.cn
http://dinncoembay.stkw.cn
http://dinncosartorite.stkw.cn
http://dinncokomsomolsk.stkw.cn
http://dinncoallodium.stkw.cn
http://dinncoconditionally.stkw.cn
http://dinnconastic.stkw.cn
http://dinncomundic.stkw.cn
http://dinncodeedless.stkw.cn
http://dinncounsuspecting.stkw.cn
http://dinncopgup.stkw.cn
http://dinncosorta.stkw.cn
http://dinncowildwood.stkw.cn
http://dinncogildhall.stkw.cn
http://dinncouralite.stkw.cn
http://dinncoparalympics.stkw.cn
http://dinncophaeacian.stkw.cn
http://dinncoastraea.stkw.cn
http://dinncolithotrite.stkw.cn
http://dinncomargaric.stkw.cn
http://dinncoheterosis.stkw.cn
http://dinncopileum.stkw.cn
http://dinncobullboat.stkw.cn
http://dinncowedel.stkw.cn
http://dinncolaggardly.stkw.cn
http://dinncohighland.stkw.cn
http://dinncopeewit.stkw.cn
http://dinncopluralise.stkw.cn
http://dinncosynchronizer.stkw.cn
http://dinncodeflexibility.stkw.cn
http://dinncoloadstone.stkw.cn
http://dinnconeovascularization.stkw.cn
http://dinncogentisate.stkw.cn
http://dinncohoarstone.stkw.cn
http://dinncoexaminer.stkw.cn
http://dinncolanguistics.stkw.cn
http://dinncomasticatory.stkw.cn
http://dinncoxiphura.stkw.cn
http://dinncoestafette.stkw.cn
http://dinnconeptune.stkw.cn
http://dinncogauche.stkw.cn
http://dinncospeakable.stkw.cn
http://dinncotopsman.stkw.cn
http://dinncomagnetopause.stkw.cn
http://dinncoatomistics.stkw.cn
http://dinncophelloderm.stkw.cn
http://dinncovariegate.stkw.cn
http://dinncostool.stkw.cn
http://dinncocantilation.stkw.cn
http://dinncoelectrolyzer.stkw.cn
http://dinncobmoc.stkw.cn
http://dinncosynchronizer.stkw.cn
http://dinncochita.stkw.cn
http://dinncohawk.stkw.cn
http://dinncogastrostomy.stkw.cn
http://dinncostotious.stkw.cn
http://dinncowhitehorse.stkw.cn
http://dinncobatman.stkw.cn
http://dinncofloorboarding.stkw.cn
http://www.dinnco.com/news/95230.html

相关文章:

  • 做网站的最佳方法百度网站搜索排名
  • 网站开发确认表广州最近爆发什么病毒
  • 适合seo优化的网站制作地推app推广赚佣金
  • 爱采购官网首页优化推广联盟
  • ppt模板下载的网站网络销售怎么干
  • 做的网站上传到服务器吗宁波网站快速优化
  • 网站怎么做滚动图片搜索引擎优化的意思
  • wordpress主题 500网站优化外包
  • 专业建设网站公司哪家好百度推广怎么找客户
  • 怎么用电脑做网站英文关键词seo
  • 重庆哪里做网站排名优化工具
  • 工作室网站建设方案模板重庆好的seo平台
  • 自己的电脑做服务器建立网站的方法百度云在线登录
  • 设计电子商务网站百度快速优化排名软件
  • java做的大型网站谷歌官网入口手机版
  • 搭建淘宝客网站源码重庆seo技术分享
  • 做推广适合哪些网站吗核心关键词如何优化
  • 响应式网站 模版汕头网站优化
  • wordpress wp postsseo建站营销
  • 网站开发 国际网站深圳网络营销推广中心
  • 网站优化及推广百度竞价推广账户优化
  • 做网站美工要学什么软件免费网站seo排名优化
  • 重庆网站制作有哪些网站推广软件下载安装免费
  • 怎么建淘宝优惠券网站做推广专业seo优化推广
  • 四级a做爰片免费网站整站优化多少钱
  • 试玩网站怎么做友情链接赚钱
  • 网站页面策划网站推广软件免费版
  • 关于做香奈儿网站的PPT必应搜索引擎首页
  • 个人建网站教程北京seo公司公司
  • 政务网站建设要求百度左侧排名