96SEO 2026-02-23 11:45 11
{System.o…当需要在方法前后做一些操作就需要借助动态代理来实现

Proxy.newProxyInstance(Test.class.getClassLoader(),
只能代理实现UserInterface接口的类不能强转成UserServiceUserInterface
(UserInterface)o;userInterface.test();
必须基于实现接口产生代理对象类型是UserInterface而不是UserService
相比jdk动态代理cglib不需要修改代码就可以实现动态代理cglib实现代理是继承被代理类的方式
Enhancer();enhancer.setSuperclass(UserService.class);//
定义额外逻辑也就是代理逻辑enhancer.setCallbacks(new
{System.out.println(before...);Object
objects);System.out.println(after...);return
NoOp.INSTANCE});enhancer.setCallbackFilter(new
(method.getName().equals(test))
动态代理所创建出来的UserService对象UserService
执行这个userService的test方法时就会额外会执行一些其他逻辑userService.test();//
调用a方法时对应过滤返回的是1NoOp.INSTANCE是空操作不会对代理对象做任何操作userService.a();
3、spring对jdk和cglib进行封装的ProxyFactory
ProxyFactory();proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
jdk动态时设置proxyFactory.addAdvice(new
{System.out.println(before...);Object
invocation.proceed();System.out.println(after...);return
proxyFactory.getProxy();userService.test();
advice方法执行完finally之后执行这是最后的比return更后Around
ProxyFactory();proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
jdk动态时设置proxyFactory.addAdvice(new
TestBeforeAdvice());UserService
proxyFactory.getProxy();userService.test();public
ProxyFactory();proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
jdk动态时设置proxyFactory.addAdvice(new
TestAfterReturningAdvice());UserService
proxyFactory.getProxy();userService.test();public
ProxyFactory();proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
jdk动态时设置proxyFactory.addAdvice(new
TestAfterThrowingAdvice());UserService
proxyFactory.getProxy();userService.test();public
{System.out.println(方法执行发生异常);}
com.zhouyu.service.UserService.test(UserService.java:25)at
com.zhouyu.service.UserService$$FastClassBySpringCGLIB$$7bfcfe0.invoke(generated)at
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)at
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:791)at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)at
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762)at
org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:113)at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:198)at
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762)at
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:704)at
com.zhouyu.service.UserService$$EnhancerBySpringCGLIB$$e9a0fc71.test(generated)at
com.zhouyu.Test.main(Test.java:25)
ProxyFactory();proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
jdk动态时设置proxyFactory.addAdvice(new
TestAroundAdvice());UserService
proxyFactory.getProxy();userService.test();public
{System.out.println(方法执行之前);Object
invocation.proceed();System.out.println(方法执行之后);return
上述的Advice只要是UserService类的方法都会被代理执行
添加自己想执行的执行的方法下面代码只会执行test方法的Advice代码
ProxyFactory();proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
TestBeforeAdvice());proxyFactory.addAdvisor(new
(method.getName().equals(test))
TestBeforeAdvice();}Overridepublic
proxyFactory.getProxy();userService.test();userService.a();
利用ProxyFactoryBean生成一个代理对象执行test方法之前执行代理逻辑
ProxyFactoryBean();proxyFactoryBean.setTarget(new
UserService());proxyFactoryBean.addAdvice(new
proxyFactoryBean;}AnnotationConfigApplicationContext
AnnotationConfigApplicationContext(AppConfig.class);UserService
applicationContext.getBean(userServiceProxy);userService.test();
beanName匹配到的将会自动创建代理对象根据设置的Advice在调用方法时执行相关代理逻辑通过beanPostProcessor把Advice添加到一个集合中当调用调用被代理类时指定的beanName的方法执行时都会执行代理逻辑
BeanNameAutoProxyCreator();beanNameAutoProxyCreator.setBeanNames(userSe*);beanNameAutoProxyCreator.setInterceptorNames(testBeforeAdvice);beanNameAutoProxyCreator.setProxyTargetClass(true);return
beanNameAutoProxyCreator;}Component
}AnnotationConfigApplicationContext
AnnotationConfigApplicationContext(AppConfig.class);UserService
applicationContext.getBean(userService);userService.test();
四、DefaultAdvisorAutoProxyCreator
defaultPointcutAdvisor(){NameMatchMethodPointcut
NameMatchMethodPointcut();pointcut.addMethodName(test);DefaultPointcutAdvisor
DefaultPointcutAdvisor();defaultPointcutAdvisor.setPointcut(pointcut);defaultPointcutAdvisor.setAdvice(new
执行beanPostProcessor时会把advisor添加到一个集合中Beanpublic
defaultAdvisorAutoProxyCreator()
{DefaultAdvisorAutoProxyCreator
DefaultAdvisorAutoProxyCreator();return
defaultAdvisorAutoProxyCreator;}AnnotationConfigApplicationContext
AnnotationConfigApplicationContext(AppConfig.class);UserService
applicationContext.getBean(userService);userService.test();
AspectJ是在编译时对字节码进行了修改是直接在UserService类对应的字节码中进行增强的也就是可以理解为是在编译时就会去解析Before这些注解然后得到代理逻辑加入到被代理的类中的字节码中去的所以如果想用AspectJ技术来生成代理对象
是需要用单独的AspectJ编译器的。
我们在项目中很少这么用我们仅仅只是用了Before这些注解而我们在启动Spring的过程中Spring会去解析这些注解然后利用动态代理机制生成代理对象的。
Aspect表示切面比如被Aspect注解的类就是切面可以在切面中去定义Pointcut、Advice等等Join
point表示连接点表示一个程序在执行过程中的一个点比如一个方法的执行比如一个异常的处理在Spring
AOP中一个连接点通常表示一个方法的执行。
Advice表示通知表示在一个特定连接点上所采取的动作。
Advice分为不同的类型后面详细讨论在很多AOP框架中包括Spring会用Interceptor拦截器来实现Advice并且在连接点周围维护一个Interceptor链Pointcut表示切点用来匹配一个或多个连接点Advice与切点表达式是关联在一起的Advice将会执行在和切点表达式所匹配的连接点上Introduction可以使用DeclareParents来给所匹配的类添加一个接口并指定一个默认实现Target
Framework中要么是JDK动态代理要么是CGLIB代理Weaving表示织入表示创建代理对象的动作这个动作可以发生在编译时期比如Aspejctj或者运行时比如Spring
BeforeAfterReturningAfterThrowingAfterAround
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback