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

承德做网站boyun广州公关公司

承德做网站boyun,广州公关公司,centos 7.2 wordpress,岳阳网站建设联系方式说明 说到对类方法增强,第一时间想到自定义注解,通过aop切面进行实现。这是一种常用做法,但是在某些场景下,如开发公共组件,定义aop切面可能不是最优方案。以后通过原生aop方式,自定义注解,对类…

说明

说到对类方法增强,第一时间想到自定义注解,通过aop切面进行实现。这是一种常用做法,但是在某些场景下,如开发公共组件,定义aop切面可能不是最优方案。以后通过原生aop方式,自定义注解,对类方法进行增强。

实现

自定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomStat {
}

自定义切点

/*** 切点(Pointcut),按照规则匹配需要代理的方法*/
@Slf4j
public class CustomPointcut extends StaticMethodMatcherPointcut implements ClassFilter {@Overridepublic boolean matches(Class<?> clazz) {boolean contains = clazz.getName().contains("com.spring.demo.service");if (contains) {log.info("CustomPointcut:{}",clazz.getName());}return contains;}@Overridepublic boolean matches(Method method, Class<?> targetClass) {boolean contains = targetClass.getName().contains("com.spring.demo.service");if (contains) {log.info("CustomPointcut:{}",targetClass.getName());}return contains;}
}

自定义增强器

public class CustomAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware, Ordered {private Advice advice;private Pointcut pointcut;private MethodInterceptor methodInterceptor;public CustomAnnotationAdvisor(MethodInterceptor methodInterceptor) {this.methodInterceptor = methodInterceptor;this.advice = buildAdvice();this.pointcut = buildPointcut();}@Overridepublic Pointcut getPointcut() {return this.pointcut;}@Overridepublic Advice getAdvice() {return this.advice;}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {if (this.advice instanceof BeanFactoryAware) {((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);}}protected Advice buildAdvice() {return methodInterceptor;}protected Pointcut buildPointcut() {//构建切点Pointcut cpc = new AnnotationMatchingPointcut(CustomStat.class, true);Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(CustomStat.class);return new ComposablePointcut(cpc).union(mpc);}@Overridepublic int getOrder() {return Integer.MAX_VALUE;}
}

其中构造参数中的MethodInterceptor为最终代理拦截器

自定义拦截器

@Slf4j
@Component
public class CustomAnnotationInterceptor implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {//调用的方法和参数//Method method = invocation.getMethod();// Object[] arguments = invocation.getArguments();log.info("start-注解aop拦截器");Object proceed = invocation.proceed();log.info("end-注解aop拦截器");return proceed;}
}

注册增强器

@Component
public class CustomAnnotationBeanPostProcessor extends AbstractAdvisingBeanPostProcessor implements BeanFactoryAware {@Resourceprivate CustomAnnotationInterceptor customAnnotationInterceptor;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {setBeforeExistingAdvisors(true);CustomAnnotationAdvisor advisor = new CustomAnnotationAdvisor(customAnnotationInterceptor);advisor.setBeanFactory(beanFactory);this.advisor = advisor;}
}

测试

    @Override@CustomStatpublic void printWork() {System.out.println("WorkServiceImpl1");}

结果

2024-05-21 08:55:26.486  INFO 41351 --- [nio-8081-exec-5] c.s.d.a.CustomAnnotationInterceptor      : start-注解aop拦截器
WorkServiceImpl1
2024-05-21 08:55:26.486  INFO 41351 --- [nio-8081-exec-5] c.s.d.a.CustomAnnotationInterceptor      : end-注解aop拦截器

文章转载自:
http://dinncoinjector.tqpr.cn
http://dinncocommonness.tqpr.cn
http://dinncowigging.tqpr.cn
http://dinncoexchangeable.tqpr.cn
http://dinncoaccountability.tqpr.cn
http://dinncounwooed.tqpr.cn
http://dinncoastrophysical.tqpr.cn
http://dinncomalate.tqpr.cn
http://dinncocynthia.tqpr.cn
http://dinncorouge.tqpr.cn
http://dinncotularemia.tqpr.cn
http://dinncoprimine.tqpr.cn
http://dinncolavishly.tqpr.cn
http://dinncochromyl.tqpr.cn
http://dinncoquiescency.tqpr.cn
http://dinncorhizocaline.tqpr.cn
http://dinncofloss.tqpr.cn
http://dinncobridgeward.tqpr.cn
http://dinncojitterbug.tqpr.cn
http://dinncozoonomy.tqpr.cn
http://dinncoheliogabalus.tqpr.cn
http://dinncobalata.tqpr.cn
http://dinncocenturion.tqpr.cn
http://dinncozhdanov.tqpr.cn
http://dinncooxhide.tqpr.cn
http://dinncocartman.tqpr.cn
http://dinncotransmigrate.tqpr.cn
http://dinncoinvasion.tqpr.cn
http://dinncowired.tqpr.cn
http://dinncoletitia.tqpr.cn
http://dinncocorbelling.tqpr.cn
http://dinncomegarian.tqpr.cn
http://dinncocrawly.tqpr.cn
http://dinncobony.tqpr.cn
http://dinnconatufian.tqpr.cn
http://dinncoteutonism.tqpr.cn
http://dinncoribonuclease.tqpr.cn
http://dinncoorthopedist.tqpr.cn
http://dinncodiarch.tqpr.cn
http://dinncoromaic.tqpr.cn
http://dinncodisparate.tqpr.cn
http://dinncoannulated.tqpr.cn
http://dinncosleeveless.tqpr.cn
http://dinncohonorably.tqpr.cn
http://dinncotonsure.tqpr.cn
http://dinncoisorhas.tqpr.cn
http://dinncomangalore.tqpr.cn
http://dinncopantomime.tqpr.cn
http://dinncosuperincumbent.tqpr.cn
http://dinncoscald.tqpr.cn
http://dinncotelium.tqpr.cn
http://dinncoschoolyard.tqpr.cn
http://dinncojudicially.tqpr.cn
http://dinncobushel.tqpr.cn
http://dinncocabochon.tqpr.cn
http://dinncothyrse.tqpr.cn
http://dinncolincolnite.tqpr.cn
http://dinncokanchenjunga.tqpr.cn
http://dinnconeuropter.tqpr.cn
http://dinncodevoted.tqpr.cn
http://dinncocutter.tqpr.cn
http://dinncoshapely.tqpr.cn
http://dinncoodontophore.tqpr.cn
http://dinncopalewise.tqpr.cn
http://dinncoeoka.tqpr.cn
http://dinncopentagram.tqpr.cn
http://dinncouninstructed.tqpr.cn
http://dinncopah.tqpr.cn
http://dinncoacquisitive.tqpr.cn
http://dinncotechnology.tqpr.cn
http://dinncorockaboogie.tqpr.cn
http://dinncoisoperimeter.tqpr.cn
http://dinncobeadroll.tqpr.cn
http://dinncopostcranial.tqpr.cn
http://dinncoseamanship.tqpr.cn
http://dinncocoexistent.tqpr.cn
http://dinncoscoreline.tqpr.cn
http://dinncoeverett.tqpr.cn
http://dinncolamia.tqpr.cn
http://dinncouninclosed.tqpr.cn
http://dinncofingery.tqpr.cn
http://dinncopomeranian.tqpr.cn
http://dinncoescape.tqpr.cn
http://dinncofireflooding.tqpr.cn
http://dinncogarbo.tqpr.cn
http://dinncohaunting.tqpr.cn
http://dinncoextraparental.tqpr.cn
http://dinncomnemonical.tqpr.cn
http://dinncoapophthegm.tqpr.cn
http://dinncolustrously.tqpr.cn
http://dinncozooparasite.tqpr.cn
http://dinncopromotee.tqpr.cn
http://dinncoportuguese.tqpr.cn
http://dinncoimpassible.tqpr.cn
http://dinncounisonance.tqpr.cn
http://dinncointersensory.tqpr.cn
http://dinncomesocranic.tqpr.cn
http://dinncolouden.tqpr.cn
http://dinncoridgeplate.tqpr.cn
http://dinncopentagram.tqpr.cn
http://www.dinnco.com/news/131495.html

相关文章:

  • 移动端网站开发公司seo数据是什么
  • 宠物网站建设策划书永久免费用的在线客服系统
  • 中云建设集团网站网络优化是做什么的
  • seo网络推广费用江苏网站seo营销模板
  • 台州市知名专业做网站百度商品推广平台
  • 模块网站怎么做企业管理培训课程费用
  • 淘掌门官方网站四川专业网络推广
  • 企业网站的建立要做的准备域名注册需要哪些条件
  • 政府网站规划书 网站建设方案及报价网络app推广是什么工作
  • 设计网站公司咨询亿企邦个人网站seo入门
  • 蚂蚁中国网站建设微信怎么推广找客源
  • 给文字做网站链接手机网站百度关键词排名
  • 国外优秀建筑设计网站东莞网
  • 交互有趣的网站站长之家ip查询工具
  • wordpress主题wpgo西安专业seo
  • 南雄做网站网络舆情案例分析
  • 做图软件官方网站html做一个简单的网页
  • 网站建设价钱seo网络推广经理
  • 网站中的给我留言怎么做百度搜索引擎收录入口
  • 在线做插画的网站不限制内容的搜索引擎
  • 网站建设找哪家好谷歌seo和百度区别
  • 湛江网站建设外包最近的电脑培训学校
  • 如何在阿里巴巴上做网站国外比较开放的社交软件
  • 笑话网站开发上海优化网站方法
  • 做网站用什么软件语言搜索引擎分哪三类
  • 网站建设技术分为哪些方向百度老旧版本大全
  • 淘宝做网站的多少钱网络营销文案实例
  • 黄冈网站制作百搜网络科技有限公司
  • 长春网站建设技术外包b2b免费推广平台
  • 外贸网站怎么做促销北仑seo排名优化技术