1. Advice, Advisor 和 Advised
Advice 表示增强,有多种增强类型 (BeforAdvice, AfterReturningAdvice 等)
package org.aopalliance.aop;
public interface Advice {
}
Advisor 持有 Advice
public interface Advisor {
Advice EMPTY_ADVICE = new Advice() {};
Advice getAdvice();
boolean isPerInstance();
}
Advised 是 AOP 代理工厂配置类接口,提供操作和管理 Advice 和 Advisor 的能力
public interface Advised extends TargetClassAware {
// ...
Advisor[] getAdvisors();
void addAdvisor(Advisor advisor) throws AopConfigException;
void addAdvice(Advice advice) throws AopConfigException;
}
2. Advice (增强)
@Configuration
public class BusinessInterceptorConfig {
@Bean
public Advice loginInterceptor() {
return new LoginInterceptor();
}
}
@Slf4j
public class LoginInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
log.info(this.getClass().getSimpleName() + "; " + invocation.getMethod().getName());
return invocation.proceed();
}
}

3. Advisor(通知器)
- 连接 Advice 和 Pointcut
@Configuration
public class BusinessInterceptorConfig {
@Bean
public Advisor businessAdvisor() {
MyInterceptor myInterceptor = new MyInterceptor();
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, InterceptAnno.class, true);
advisor.setPointcut(pointcut);
advisor.setAdvice(myInterceptor);
return advisor;
}
}
或者
@Configuration
public class MyInterceptorConfig {
@Bean
public Advice myInterceptor() {
return new MyInterceptor();
}
@Bean
public Advisor pointcutAdvisor() {
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
// 设置 checkInherited = true
// AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, InterceptAnno.class, true);
AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(InterceptAnno.class, true);
advisor.setPointcut(pointcut);
advisor.setAdvice(myInterceptor());
return advisor;
}
}
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
本平台(www.cooy.cn)的一切软件、教程及内容信息仅限用于学习和研究,付费仅为收集整理归类费用;
不得将上述内容用于商业或者非法用途,否则一切后果用户自行承担负责。本平台资源、内容、信息均来自来自用户上传,版权争议及其他问题与本平台无关。
您必须在下载后的24个小时之内从您的电脑或手机中彻底删除上述下载内容,如果您喜欢该程序或内容,请支持正版以获取更好的服务。我们非常重视版权问题,如有侵权请发送邮件至下方邮件(655465@qq.com),敬请谅解!
如发现违法违规内容,请联系下方邮箱举报,我们收到后将会第一时间处理。
THE END
暂无评论内容