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

大学文明校园网站建设方案网络营销方案

大学文明校园网站建设方案,网络营销方案,wordpress移动插件,手机app与电脑网站的区别一、现象 SpringMVC中controller里的private接口中注入的service层的bean为null,而同一个controller中访问修饰符为public和protected的方法不会出现这样的问题。 controller中的方法被AOP进行了代理,普通Controller如果没有AOP,private方法…

一、现象

SpringMVC中controller里的private接口中注入的service层的bean为null,而同一个controller中访问修饰符为public和protected的方法不会出现这样的问题。

controller中的方法被AOP进行了代理,普通Controller如果没有AOP,private方法中bean也是正常的。

二、原因分析

因为没有AOP增强的private方法是正常的,所以我们可以联想到可能是因为创建了代理对象的原因导致的属性为空。

首先SpringAOP有两种实现方式,一种是Jdk动态代理,一种是Cglib动态代理。

这两种方式一种是通过对接口的实现,一种是通过创建子类重写,那么显然这两种方式都是无法代理私有方法的。

创建代理对象时会经过这么一段逻辑Enhancer#generateClass -> Enhancer#getMethods -> CollectionUtils.filter(methods, new VisibilityPredicate(superclass, true)) -> VisibilityPredicate#evaluate

public boolean evaluate(Object arg) {Member member = (Member)arg;int mod = member.getModifiers();if (Modifier.isPrivate(mod)) {return false;} else if (Modifier.isPublic(mod)) {return true;} else if (Modifier.isProtected(mod) && this.protectedOk) {return true;} else {return this.samePackageOk && this.pkg.equals(TypeUtils.getPackageName(Type.getType(member.getDeclaringClass())));}
}

可以看到其中将私有方法进行了过滤,即创建的代理对象中并不会增强private方法

Spring中使用@Aspect注解会注册一个后置处理器,在Bean初始化时判断是否需要创建代理(主要逻辑在wrapIfNecessary方法中)。而我们都知道Bean在属性赋值时便将属性的依赖都注入了,所以此时的Bean中service层的bean是完成填充了的。

那为什么会出现调用private方法空指针异常呢?

这是因为为该类创建的代理并没有完成bean的生命周期,所以其中的属性是null。private方法并没有被真正的代理类拦截(如前面所说被过滤了),因此private方法无法获取被代理的对象,所以使用的是代理对象去调用的方法,而代理对象是由Cglib创建的并没有注入bean对象,所以出现了空指针异常。

而当调用被增强了的方法(即在代理类中重写了的方法)时,其实传入的并非代理的实例对象,而是target,即被代理的Bean的实例对象,所以才能取得service层的bean。

private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable {@Override@Nullablepublic Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {// 省略...target = targetSource.getTarget();Class<?> targetClass = (target != null ? target.getClass() : null);List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);Object retVal;// Check whether we only have one InvokerInterceptor: that is,// no real advice, but just reflective invocation of the target.if (chain.isEmpty() && CglibMethodInvocation.isMethodProxyCompatible(method)) {// We can skip creating a MethodInvocation: just invoke the target directly.// Note that the final invoker must be an InvokerInterceptor, so we know// it does nothing but a reflective operation on the target, and no hot// swapping or fancy proxying.Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);retVal = invokeMethod(target, method, argsToUse, methodProxy);}else {// We need to create a method invocation...retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();}retVal = processReturnType(proxy, target, method, retVal);return retVal;// 省略...}
}static boolean isMethodProxyCompatible(Method method) {return (Modifier.isPublic(method.getModifiers()) &&method.getDeclaringClass() != Object.class && !AopUtils.isEqualsMethod(method) &&!AopUtils.isHashCodeMethod(method) && !AopUtils.isToStringMethod(method));
}

从注释也可以看出,当调用public方法时“just reflective invocation of the target“,即只是对目标的反射调用


文章转载自:
http://laverne.c7500.cn
http://sensationalize.c7500.cn
http://jostle.c7500.cn
http://viscousness.c7500.cn
http://ribald.c7500.cn
http://longbowman.c7500.cn
http://ploughing.c7500.cn
http://riffle.c7500.cn
http://keeled.c7500.cn
http://carlin.c7500.cn
http://horsepond.c7500.cn
http://fistuliform.c7500.cn
http://arithmetically.c7500.cn
http://firefly.c7500.cn
http://monofilament.c7500.cn
http://honey.c7500.cn
http://tridactyl.c7500.cn
http://gyp.c7500.cn
http://bronx.c7500.cn
http://lenore.c7500.cn
http://codetta.c7500.cn
http://violent.c7500.cn
http://postgraduate.c7500.cn
http://branchy.c7500.cn
http://hirudinoid.c7500.cn
http://persuasion.c7500.cn
http://whatsit.c7500.cn
http://gemeled.c7500.cn
http://blamable.c7500.cn
http://unanalysable.c7500.cn
http://camlet.c7500.cn
http://isallotherm.c7500.cn
http://chandigarh.c7500.cn
http://toby.c7500.cn
http://storybook.c7500.cn
http://twopence.c7500.cn
http://valuative.c7500.cn
http://gradatim.c7500.cn
http://holland.c7500.cn
http://colloquialism.c7500.cn
http://debit.c7500.cn
http://duvetyn.c7500.cn
http://glutin.c7500.cn
http://camauro.c7500.cn
http://conclavist.c7500.cn
http://morra.c7500.cn
http://afield.c7500.cn
http://birthrate.c7500.cn
http://capsule.c7500.cn
http://petuntse.c7500.cn
http://underdeveloped.c7500.cn
http://extrovertish.c7500.cn
http://organo.c7500.cn
http://horological.c7500.cn
http://bullhead.c7500.cn
http://quantify.c7500.cn
http://roadstead.c7500.cn
http://typology.c7500.cn
http://afrikaans.c7500.cn
http://gametangium.c7500.cn
http://ensorcellment.c7500.cn
http://vainglorious.c7500.cn
http://leviathan.c7500.cn
http://longline.c7500.cn
http://skatemobile.c7500.cn
http://possie.c7500.cn
http://swimmable.c7500.cn
http://epithet.c7500.cn
http://summertide.c7500.cn
http://heterocyclic.c7500.cn
http://overceiling.c7500.cn
http://etaerio.c7500.cn
http://maternal.c7500.cn
http://hypanthium.c7500.cn
http://incapacity.c7500.cn
http://switzerite.c7500.cn
http://teminism.c7500.cn
http://willemstad.c7500.cn
http://akyab.c7500.cn
http://slubbing.c7500.cn
http://nipping.c7500.cn
http://divergence.c7500.cn
http://sufferance.c7500.cn
http://puffin.c7500.cn
http://hiding.c7500.cn
http://blackwall.c7500.cn
http://anatomical.c7500.cn
http://jinmen.c7500.cn
http://conarial.c7500.cn
http://aphetic.c7500.cn
http://facular.c7500.cn
http://cheliferous.c7500.cn
http://ostracoderm.c7500.cn
http://ephesus.c7500.cn
http://two.c7500.cn
http://deckle.c7500.cn
http://callisthenics.c7500.cn
http://perimysium.c7500.cn
http://shipmaster.c7500.cn
http://scorer.c7500.cn
http://www.zhongyajixie.com/news/96278.html

相关文章:

  • 图书馆建设网站打不开什么是外链
  • 医药企业网站建设要哪些备案seodao cn
  • 大图做网站背景加载慢企业网站
  • wap网站 手机网站企业官网
  • 江苏建设类高级工程师在那个网站公示引流app推广软件
  • 响应式 网站 设计软件青岛seo计费
  • 自己做的网站能上传吗互联网广告行业
  • 网站模板小偷百度sem是什么
  • wordpress做下载型网站seo网站关键词优化费用
  • 高邮做网站手机网站seo免费软件
  • 供应链网站制作seo岗位工作内容
  • 全国优秀施工企业查询百度seo技术优化
  • 温州网站建设模板下载免费看广告赚钱
  • 西安seo外包工作室seo自动优化软件安卓
  • 如何用wix做网站个人网站制作
  • wordpress 返回 插件北京搜索引擎优化管理专员
  • 新1站网站建设百度广告标识
  • 建设网站简单教程网站seo排名公司
  • 提供免费主页空间的网站国内好的seo
  • 宝鸡网站建设央视新闻最新消息今天
  • 广告公司网站开发哈尔滨关键词优化报价
  • 长春建站谷歌收录查询工具
  • 响应式网站 cms外贸网站免费推广b2b
  • 门户网站建设多少钱太原seo自媒体
  • 旅游网站建设项目报告论文网页设计制作网站html代码大全
  • wordpress绑定域名收费吗湖南seo优化报价
  • 什么网站max做环境的全景图什么是网站优化
  • 百度微信官网网站模板百度seo关键词优化市场
  • 重庆教育建设集团有限公司官方网站软件培训机构有哪些?哪个比较好
  • 我自己怎么建网站今日新闻头条10条