沈阳世纪兴网站制作公司,住房城乡建设厅网站,wordpress 影视,网站维护中页面模板前言spring一直以来都是我们Java开发中最核心的一个技术#xff0c;其中又以ioc和aop为主要技术#xff0c;本篇文章主要讲一下aop的核心技术#xff0c;也就是ProxyFactory技术的使用#xff0c;而基本的jdk动态代理和cglib代理技术并不涉及#xff0c;如有需要#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的话就可以借助如下的工具类如ProxyFactorypackage 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对象PointcutPointcut简单理解就是切掉也就是用于判断要在哪些方法或者哪些类注入代理逻辑用的Advisor而Advisor简单理解就是Advice和Pointcut的组合spring当中进行代理的逻辑也是用Advisor为维度进行处理的以上就是使用ProxyFactory进行代理逻辑的spring工具类但是很明显这样使用相对来说还是比较麻烦的所以spring提供了简易的方式让我们使用这种逻辑如下Spring提供的代理支持ProxyFactoryBeanpackage 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回去但是还有更简单的方式如下DefaultPointcutAdvisorpackage 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流程进行理解就方便多了