重庆 网站建设,手工网站怎样做三角包,wordpress swatch,wordpress扩展性想要环绕拦截一个 Bean 的函数。需要三个前置条件#xff1a;
通过注解做为“切点”#xff0c;进行拦截#xff08;不能无缘无故给拦了吧#xff1f;费性能#xff09;Bean 的 method 是被代理的在 Bean 被扫描之前#xff0c;完成环绕拦截的注册
1、定义切点和注册环…想要环绕拦截一个 Bean 的函数。需要三个前置条件
通过注解做为“切点”进行拦截不能无缘无故给拦了吧费性能Bean 的 method 是被代理的在 Bean 被扫描之前完成环绕拦截的注册
1、定义切点和注册环绕拦截
Solon 的切点通过注解实现得先定义一个。例如Logging
//Target 是决定可以注在什么上面的
Target({ElementType.METHOD, ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
public interface Logging {boolean enable() default true;
}定义拦截器
//简单点处理
Slf4j
public class LoggingInterceptor implements Interceptor {Overridepublic Object doIntercept(Invocation inv) throws Throwable {//此处为拦截处理Object rst inv.invoke();log.info(Args: {}\nReturn: {}, inv.args(), rst);return rst;}
}//如果需要取注解信息并进行控制
Slf4j
public class LoggingInterceptor2 implements Interceptor {Overridepublic Object doIntercept(Invocation inv) throws Throwable {Logging anno inv.method().getAnnotation(Logging.class);if (anno null) {//因为 Logging 支持 ElementType.TYPE所以也要检查类上的注解anno inv.target().getClass().getAnnotation(Logging.class);}//此处为拦截处理Object rst inv.invoke();if(anno ! null anno.enable()){log.info(Args: {}\nReturn: {}, inv.args(), rst);}return rst;}
}手动注册或关联绑定环绕拦截二种模式选一即可
//手动注册模式
Solon.context().beanAroundAdd(Logging.class, new LoggingInterceptor());//关联绑定模式通过Around注解直接在注解类上关联绑定
Around(LoggingInterceptor.class)
Target({ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface Logging {
}现在切点定义好了可以到处“埋”了…
2、应用把切点“埋”到需要的地方
Service
public class DemoController{Loggingpublic void addUser(UserModel user){//...}
}就这样完成一个面向切面的开发了。
3、通过插件及插件配置变成一个复用的东西
这是刚才定义注解
Target({ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface Logging {
}开发插件
Slf4j
public class XPluginImp implements Plugin {Overridepublic void start(AopContext context) {context.beanAroundAdd(Logging.class, inv-{Object rst inv.invoke();log.info(Args: {}\nReturn: {}, inv.args(), rst);});}
}配置插件
solon.pluginxxx.xxx.log.XPluginImp一个可复用的插件开发完成了。关于Solon插件开发可参考别的章节内容。