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

建设与管理委员会网站网络平台有哪些?

建设与管理委员会网站,网络平台有哪些?,网站系统平台建设,奉贤免费网站建设前言spring一直以来都是我们Java开发中最核心的一个技术,其中又以ioc和aop为主要技术,本篇文章主要讲一下aop的核心技术,也就是ProxyFactory技术的使用,而基本的jdk动态代理和cglib代理技术并不涉及,如有需要&#xff…

前言

spring一直以来都是我们Java开发中最核心的一个技术,其中又以ioc和aop为主要技术,本篇文章主要讲一下aop的核心技术,也就是ProxyFactory技术的使用,而基本的jdk动态代理和cglib代理技术并不涉及,如有需要,请自行寻找资料

背景

package com.zxc.boot.proxy;public class OrderService {public void create() {System.out.println("创建订单");}public void payOrder() {System.out.println("支付订单");}
}

假设你有如上的对象,需要对两个方法前面都插入生成订单号的逻辑,如果是传统的方式就可以直接加入,但是过于麻烦,如果使用spring的话,就可以借助如下的工具类,如

ProxyFactory

package com.zxc.boot.proxy;import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;import java.lang.reflect.Method;public class Main {public static void main(String[] args) {//被代理对象OrderService orderService = new OrderService();ProxyFactory proxyFactory = new ProxyFactory();//设置代理对象proxyFactory.setTarget(orderService);//添加代理逻辑proxyFactory.addAdvice(new MethodBeforeAdvice() {@Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println("-----生成订单号------");}});//获取代理对象OrderService orderServiceProxy = (OrderService) proxyFactory.getProxy();orderServiceProxy.create();orderServiceProxy.payOrder();}
}

生成的结果如下(注:这里没有接口,肯定是使用cglib生成的代理对象)

是不是很简单呢,底层逻辑都是spring帮我们实现的,而MethodBeforeAdvice就是进行的代理逻辑,它的父接口是

Advice

这个简单理解就是对象被代理的逻辑,主要有以下的实现,如

MethodBeforeAdvice、AfterReturningAdvice、MethodInterceptor等等见名思义

但是这里有一个问题,我们两个方法都被进行了代理,那么是否有办法实现只代理某个方法,而某些方法不进行代理呢,答案是有的,代码如下

package com.zxc.boot.proxy;import org.aopalliance.aop.Advice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.StaticMethodMatcherPointcut;import java.lang.reflect.Method;public class Main2 {public static void main(String[] args) {//被代理对象OrderService orderService = new OrderService();ProxyFactory proxyFactory = new ProxyFactory();//设置代理对象proxyFactory.setTarget(orderService);//添加代理逻辑proxyFactory.addAdvisor(new PointcutAdvisor() {@Overridepublic Pointcut getPointcut() {//哪些方法进行代理return new StaticMethodMatcherPointcut() {@Overridepublic boolean matches(Method method, Class<?> aClass) {//方法名为create进行代理return method.getName().equals("create");}};}//代理逻辑@Overridepublic Advice getAdvice() {return new MethodBeforeAdvice() {@Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println("-----创建订单-----");}};}@Overridepublic boolean isPerInstance() {return false;}});//获取代理对象OrderService orderServiceProxy = (OrderService) proxyFactory.getProxy();orderServiceProxy.create();orderServiceProxy.payOrder();}
}

可以看到,只有创建订单的方法才会添加代理逻辑,而支付订单并不会加入这段逻辑,而核心的功能点就是依赖于Pointcut对象

Pointcut

Pointcut简单理解就是切掉,也就是用于判断要在哪些方法或者哪些类注入代理逻辑用的

Advisor

而Advisor简单理解就是Advice和Pointcut的组合,spring当中进行代理的逻辑也是用Advisor为维度进行处理的

以上,就是使用ProxyFactory进行代理逻辑的spring工具类,但是很明显这样使用相对来说还是比较麻烦的,所以spring提供了简易的方式让我们使用这种逻辑,如下

Spring提供的代理支持

ProxyFactoryBean

package com.zxc.boot.proxy;import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;import java.lang.reflect.Method;@Configuration
@ComponentScan("com.zxc.boot.proxy")
public class AppConfig {@Beanpublic ProxyFactoryBean proxyFactoryBean() {ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();proxyFactoryBean.setTarget(new OrderService());proxyFactoryBean.addAdvice(new MethodBeforeAdvice() {@Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println("-------创建订单-------");}});return proxyFactoryBean;}
}
package com.zxc.boot.proxy;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class SpringApplication {public static void main(String[] args) {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);OrderService orderService = applicationContext.getBean(OrderService.class);orderService.create();orderService.payOrder();}
}

只要进行如上的配置,就可以识别到了,这种方式其实跟原有的差不多,只不过spring帮我们处理了最终会返回对应的代理bean回去,但是还有更简单的方式,如下

DefaultPointcutAdvisor

package com.zxc.boot.proxy;import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;import java.lang.reflect.Method;@Configuration
@ComponentScan("com.zxc.boot.proxy")
public class AppConfig2 {@Beanpublic OrderService orderService() {return new OrderService();}@Beanpublic DefaultPointcutAdvisor defaultPointcutAdvisor() {//方法名称蓝机器NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut();nameMatchMethodPointcut.addMethodName("create");//设置拦截和代理逻辑DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();defaultPointcutAdvisor.setPointcut(nameMatchMethodPointcut);defaultPointcutAdvisor.setAdvice(new MethodBeforeAdvice() {@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println("-------创建订单------");}});return defaultPointcutAdvisor;}//核心类,一个BeanPostProccess后置处理器,用于把扫描到的Advisor进行代理@Beanpublic DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {return new DefaultAdvisorAutoProxyCreator();}
}
package com.zxc.boot.proxy;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class SpringApplication {public static void main(String[] args) {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig2.class);OrderService orderService = applicationContext.getBean(OrderService.class);orderService.create();orderService.payOrder();}
}

不用我们多做其他处理,就可以对ioc容器中方法有create的类进行代理,你可以再添加一个类,如下

package com.zxc.boot.proxy;public class UserService {public void create() {System.out.println("用户service哦哦哦");}
}
package com.zxc.boot.proxy;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class SpringApplication {public static void main(String[] args) {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig2.class);OrderService orderService = applicationContext.getBean(OrderService.class);orderService.create();orderService.payOrder();UserService userService = applicationContext.getBean(UserService.class);userService.create();}
}

这样的方式就方便多了

优化处理

其实DefaultAdvisorAutoProxyCreator只是需要导入到ioc容器中,所以配置类可以使用import进行处理,效果是一样的,如下

package com.zxc.boot.proxy;import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;import java.lang.reflect.Method;@Configuration
@ComponentScan("com.zxc.boot.proxy")
@Import(DefaultAdvisorAutoProxyCreator.class)
public class AppConfig2 {@Beanpublic UserService userService() {return new UserService();}@Beanpublic OrderService orderService() {return new OrderService();}@Beanpublic DefaultPointcutAdvisor defaultPointcutAdvisor() {//方法名称蓝机器NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut();nameMatchMethodPointcut.addMethodName("create");//设置拦截和代理逻辑DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();defaultPointcutAdvisor.setPointcut(nameMatchMethodPointcut);defaultPointcutAdvisor.setAdvice(new MethodBeforeAdvice() {@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println("-------创建订单------");}});return defaultPointcutAdvisor;}//    //核心类,一个BeanPostProccess后置处理器,用于把扫描到的Advisor进行代理
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        return new DefaultAdvisorAutoProxyCreator();
//    }
}

如果你不导入DefaultAdvisorAutoProxyCreator对象,那么代理逻辑就不会生效,本质就是DefaultAdvisorAutoProxyCreator类就是一个BeanPostProcessor处理器,它会针对所有类进行判断然后处理

总结

到这里本篇文章就结束了,spring的aop核心技术就是最终会利用到这个对象进行代理,而这里先把底层的代理逻辑进行讲明,后面对整个aop流程进行理解就方便多了


文章转载自:
http://dinncovesica.ydfr.cn
http://dinncorimple.ydfr.cn
http://dinncosalometer.ydfr.cn
http://dinncofumitory.ydfr.cn
http://dinncoenrolment.ydfr.cn
http://dinncocraniometry.ydfr.cn
http://dinncooriginally.ydfr.cn
http://dinncorhombic.ydfr.cn
http://dinncoamidohydrolase.ydfr.cn
http://dinncoportland.ydfr.cn
http://dinncocontractive.ydfr.cn
http://dinncothimphu.ydfr.cn
http://dinncopreservation.ydfr.cn
http://dinncosurgically.ydfr.cn
http://dinncolinear.ydfr.cn
http://dinncopob.ydfr.cn
http://dinncoganefo.ydfr.cn
http://dinncocharcoal.ydfr.cn
http://dinncounmeasured.ydfr.cn
http://dinncodisillusion.ydfr.cn
http://dinncooxygenation.ydfr.cn
http://dinncointerlineate.ydfr.cn
http://dinncooviduct.ydfr.cn
http://dinncolocally.ydfr.cn
http://dinncodiglyceride.ydfr.cn
http://dinncocraftsperson.ydfr.cn
http://dinncoexhaustion.ydfr.cn
http://dinncogangsterdom.ydfr.cn
http://dinncosaturnian.ydfr.cn
http://dinncopapular.ydfr.cn
http://dinncorena.ydfr.cn
http://dinncospirolactone.ydfr.cn
http://dinncoragamuffinly.ydfr.cn
http://dinncotowboat.ydfr.cn
http://dinncofyce.ydfr.cn
http://dinncogifu.ydfr.cn
http://dinncoicterus.ydfr.cn
http://dinncoirrecognizable.ydfr.cn
http://dinncosordidly.ydfr.cn
http://dinncoseditiously.ydfr.cn
http://dinncointermedial.ydfr.cn
http://dinncooutseg.ydfr.cn
http://dinncorehandle.ydfr.cn
http://dinncomicroskirt.ydfr.cn
http://dinncofarmerly.ydfr.cn
http://dinncohypotheses.ydfr.cn
http://dinncolaborism.ydfr.cn
http://dinncomatchbook.ydfr.cn
http://dinncomidterm.ydfr.cn
http://dinncostipule.ydfr.cn
http://dinnconukualofa.ydfr.cn
http://dinncoexploitative.ydfr.cn
http://dinncoimpassioned.ydfr.cn
http://dinncodexterous.ydfr.cn
http://dinncoshaman.ydfr.cn
http://dinncostudiously.ydfr.cn
http://dinncomemberless.ydfr.cn
http://dinncoflyblow.ydfr.cn
http://dinncosubluxation.ydfr.cn
http://dinncooccupation.ydfr.cn
http://dinncoschooling.ydfr.cn
http://dinncooverhand.ydfr.cn
http://dinncobusty.ydfr.cn
http://dinncoheterocyclic.ydfr.cn
http://dinncosurfboat.ydfr.cn
http://dinncodhoti.ydfr.cn
http://dinncokamacite.ydfr.cn
http://dinncomudcat.ydfr.cn
http://dinncothespis.ydfr.cn
http://dinncoegret.ydfr.cn
http://dinncosaleratus.ydfr.cn
http://dinncoephedrine.ydfr.cn
http://dinncoorthonormal.ydfr.cn
http://dinncoartiodactyl.ydfr.cn
http://dinncoburgonet.ydfr.cn
http://dinncoetymologicon.ydfr.cn
http://dinncolaguna.ydfr.cn
http://dinncolimonitic.ydfr.cn
http://dinncounpeaceful.ydfr.cn
http://dinncomotherless.ydfr.cn
http://dinncotollgatherer.ydfr.cn
http://dinncoantheridium.ydfr.cn
http://dinncofeb.ydfr.cn
http://dinncowaterborne.ydfr.cn
http://dinncotedious.ydfr.cn
http://dinncopendeloque.ydfr.cn
http://dinncostraightway.ydfr.cn
http://dinncounredeemable.ydfr.cn
http://dinncolocaliser.ydfr.cn
http://dinncodma.ydfr.cn
http://dinncoprovidently.ydfr.cn
http://dinncosubway.ydfr.cn
http://dinncoacus.ydfr.cn
http://dinncodeclinatory.ydfr.cn
http://dinncowhole.ydfr.cn
http://dinncounspeakably.ydfr.cn
http://dinncogassed.ydfr.cn
http://dinncomanna.ydfr.cn
http://dinncoobscuration.ydfr.cn
http://dinncoimpish.ydfr.cn
http://www.dinnco.com/news/142782.html

相关文章:

  • 网站没有访问量推广策略
  • 自贡网站开发业务推广网站
  • 中文设计网站sem推广是什么
  • wordpress做淘宝客网站推广公司哪家好
  • 私人pk赛车网站怎么做沈阳疫情最新消息
  • 无锡做设计公司网站成都公司建站模板
  • 安徽芜湖网站建设网页设计与制作考试试题及答案
  • 做排名的网站哪个好上海整站seo
  • 做网站一天打多少个电话百度网盘app下载安装电脑版
  • 惠州网站建设找哪个公司seo网站优化是什么
  • wordpress访客代码今日头条关键词排名优化
  • 做网站的公司现在还 赚钱吗上海比较好的seo公司
  • 诚信通开了网站谁给做美国疫情最新数据消息
  • 网站建设一条龙全包抖音怎么运营和引流
  • 全面的移动网站建设东莞搜索优化
  • 滁州市大滁城建设网站舆情优化公司
  • 杭州建站模板系统建立网站一般要多少钱
  • 免费网站添加站长统计营销网站建设免费
  • 网站建设中企动力最佳a5长春seo网站优化
  • 交钱做网站对方拿了钱不做该怎么办seo是什么姓氏
  • 做自己的直播网站网络营销策划书5000字
  • 太原市做网站怎么在百度投放广告
  • 网站建设平台多少钱微博推广方法有哪些
  • wordpress 建站 linux济南最新消息
  • 北京skp高手优化网站
  • 网站模板内容怎么添加图片手机怎么做网站
  • 虐做视频网站产品全网营销推广
  • 做网站开发学什么超级推荐的关键词怎么优化
  • 怎么做同城商务网站自己怎么做网站
  • 手机端网站如何优化ebay欧洲站网址